eXistDB简单通讯03

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

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、QueryFile.java

package com.neohope.existdb.test;

import org.exist.util.serializer.SAXSerializer;
import org.exist.util.serializer.SerializerPool;
import org.exist.xmldb.XmldbURI;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.CompiledExpression;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.modules.XQueryService;

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

public class QueryFile extends Base{
    public static void QueryXML(String query,String user,String pwd) {
        try {
            if(cl==null)throw(new ClassNotFoundException(driver));

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

            Collection col =
                    DatabaseManager.getCollection(URI + XmldbURI.ROOT_COLLECTION,user,pwd);
            XQueryService service =
                    (XQueryService) col.getService( "XQueryService", "1.0" );
            service.setProperty( OutputKeys.INDENT, "yes" );
            service.setProperty( OutputKeys.ENCODING, "UTF-8" );

            CompiledExpression compiled = service.compile(query);

            // execute query
            long start = System.currentTimeMillis();
            ResourceSet result = service.execute( compiled );
            long qtime = System.currentTimeMillis() - start;

            start = System.currentTimeMillis();
            SAXSerializer serializer = getSAXSerializer();
            for ( int i = 0; i < (int) result.getSize(); i++ ) {
                XMLResource resource = (XMLResource) result.getResource( (long) i );
                resource.getContentAsSAX(serializer);
                System.out.println(resource.getContent());
            }
            SerializerPool.getInstance().returnObject(serializer);
            long rtime = System.currentTimeMillis() - start;

            System.out.println("hits:          " + result.getSize());
            System.out.println("query time:    " + qtime);
            System.out.println("retrieve time: " + rtime);

        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) throws Exception {
        String user = "neotest";
        String pwd = "neotest";
        String query = "";
        /*
        query  = "collection('/db/ZS_FrontPageBA')/ClinicalDocument/title";
        query  = "collection('/db/ZS_FrontPageBA')/ClinicalDocument/recordTarget/patientRole/id[@assigningAuthorityName='XDS.Patientid' and @root='2.16.840.1.113883.4.487.2.1.4']";
        */
        query = "xquery version \"3.0\";\n" +
                "for $apatient in collection('/db/ZS_FrontPageBA')/ClinicalDocument/recordTarget/patientRole\n" +
                "return\n" +
                "    <patient>\n" +
                "        <name value='{$apatient/patient/name}'></name>\n" +
                "        <sex value='{$apatient/patient/administrativeGenderCode/@displayName}'></sex>\n" +
                "        <birthTime value='{$apatient/patient/birthTime}'></birthTime>\n" +
                "        <address value='{$apatient/addr[@use='H']/streetAddressLine}'></address>\n" +
                "        <ethnic value='{$apatient/patient/ethnicGroupCode/@displayName}'></ethnic>\n" +
                "        <patientId value='{$apatient/id[@assigningAuthorityName='XDS.Patientid' and @root='2.16.840.1.113883.4.487.2.1.4']/@extension}'> </patientId>\n" +
                "        <flowId value='{$apatient/id[@assigningAuthorityName='XDS.Patientid' and @root='2.16.840.1.113883.4.487.2.1.4.4']/@extension}'> </flowId>\n" +
                "    </patient>";

        QueryXML(query,user,pwd);
    }
}

Leave a Reply

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

*