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());
        }
    }
}
复制代码

 

 5、如果运行过程中XML比较大(20M),可能会出现 java.lang.OutOfMemoryError: Java heap space 的错误

这时候就需要在Eclipse ->run -arguments 中的VM arguments 中输入-Xms32m -Xmx800m这个参数就可以了。

 

 

posted @   观我生,君子无咎  阅读(1140)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示