eXistDB简单通讯02

  • 保存文件
  • 取回文件
  • 查询
  • 嵌套查询

1、Base.java

package com.neohope.existdb.test;

import org.exist.util.serializer.SAXSerializer;
import org.exist.util.serializer.SerializerPool;

import javax.xml.transform.OutputKeys;
import java.io.OutputStreamWriter;
import java.util.Properties;

public class Base {
    protected final static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
    protected final static String driver = "org.exist.xmldb.DatabaseImpl";
    protected static  Class<?> cl = null;

    static{
        try {
            cl = Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected static SAXSerializer getSAXSerializer()
    {
        Properties outputProperties = new Properties();
        outputProperties.setProperty(OutputKeys.INDENT, "yes");
        SAXSerializer serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        serializer.setOutput(new OutputStreamWriter(System.out), outputProperties);
        return serializer;
    }

    protected static void releaseSAXSerializer(SAXSerializer serializer) {
        SerializerPool.getInstance().returnObject(serializer);
    }
}

2、GetFile.java

package com.neohope.existdb.test;

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 javax.xml.transform.OutputKeys;

public class GetFile extends Base{
    public static void GetXML(String collectionName,String documentId, String user, String pwd) throws Exception {
        if(cl==null)throw(new ClassNotFoundException(driver));

        Database database = (Database) cl.newInstance();
        database.setProperty("ssl-enable", "false");
        DatabaseManager.registerDatabase(database);

        Collection collection = DatabaseManager.getCollection(URI + collectionName,user,pwd);
        if(collection == null) {
            System.out.println("collection not found: " + collectionName);
        }
        else
        {
            collection.setProperty(OutputKeys.INDENT, "yes");
            collection.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "no");
            collection.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");
            XMLResource res = (XMLResource) collection.getResource(documentId);
            if (res == null) {
                System.out.println("document not found: "+ documentId);
            } else {
                System.out.println(res.getContent());
                System.out.println("Size: " + ((EXistResource) res).getContentLength());
            }
        }
    }

    public static void main(String args[]) throws Exception {
        String user = "neotest";
        String pwd = "neotest";
        GetXML("/db/ZS_FrontPageBA", "病案首页01.xml",user,pwd);
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

*