eXist-DB开发
1、启动Eclipse新建一个项目
2、导入exist-db的lib
3、启动exist-db服务,启动客户端数据库管理器,导入例子Shakespeare数据
新建文集
导入数据,全选然后导入
4、编写源代码

package org.xml.conn;
import javax.xml.transform.OutputKeys;
import org.exist.storage.serializers.EXistOutputKeys;
import org.exist.xmldb.EXistResource;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.modules.XMLResource;
import org.exist.xmldb.XQueryService;
import org.exist.xmldb.XPathQueryServiceImpl;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
import org.exist.xmldb.DatabaseInstanceManager;
public class conndb {
protected static String driver = "org.exist.xmldb.DatabaseImpl";
//protected static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
protected static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
protected static void usage() {
System.out.println("usage: org.exist.examples.xmldb.Retrieve collection docName");
System.exit(0);
}
public static void main(String args[]) throws Exception {
//if (args.length < 2) {
// usage();
//}
//String collection = args[0];
String collection = "/db/shakespeare";
// initialize database drivers
Class cl = Class.forName(driver);
Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);
// get the collection
Collection col = DatabaseManager.getCollection(URI + collection);
//Collection col = DatabaseManager.getCollection("xmldb:exist:///db");
col.setProperty(OutputKeys.INDENT, "yes");
col.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "no");
col.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");
//XMLResource res = (XMLResource)col.getResource(args[1]);
XMLResource res = (XMLResource)col.getResource("macbeth.xml");
if(res == null)
System.out.println("document not found!");
else {
System.out.println(res.getContent());
System.out.println("Size: " + ((EXistResource)res).getContentLength());
}
}
import javax.xml.transform.OutputKeys;
import org.exist.storage.serializers.EXistOutputKeys;
import org.exist.xmldb.EXistResource;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.modules.XMLResource;
import org.exist.xmldb.XQueryService;
import org.exist.xmldb.XPathQueryServiceImpl;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
import org.exist.xmldb.DatabaseInstanceManager;
public class conndb {
protected static String driver = "org.exist.xmldb.DatabaseImpl";
//protected static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
protected static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
protected static void usage() {
System.out.println("usage: org.exist.examples.xmldb.Retrieve collection docName");
System.exit(0);
}
public static void main(String args[]) throws Exception {
//if (args.length < 2) {
// usage();
//}
//String collection = args[0];
String collection = "/db/shakespeare";
// initialize database drivers
Class cl = Class.forName(driver);
Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);
// get the collection
Collection col = DatabaseManager.getCollection(URI + collection);
//Collection col = DatabaseManager.getCollection("xmldb:exist:///db");
col.setProperty(OutputKeys.INDENT, "yes");
col.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "no");
col.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");
//XMLResource res = (XMLResource)col.getResource(args[1]);
XMLResource res = (XMLResource)col.getResource("macbeth.xml");
if(res == null)
System.out.println("document not found!");
else {
System.out.println(res.getContent());
System.out.println("Size: " + ((EXistResource)res).getContentLength());
}
}
}
5、如果运行过程中XML比较大(20M),可能会出现 java.lang.OutOfMemoryError: Java heap space 的错误
这时候就需要在Eclipse ->run -arguments 中的VM arguments 中输入-Xms32m -Xmx800m这个参数就可以了。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步