- 保存文件
- 取回文件
- 查询
- 嵌套查询
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、QueryFileNested.java
package com.neohope.existdb.test; import org.exist.util.serializer.SAXSerializer; import org.exist.util.serializer.SerializerPool; import org.exist.xmldb.XPathQueryServiceImpl; 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 QueryFileNested extends Base{ public static void QueryXMLNested(String query1,String query2,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); XPathQueryServiceImpl service = (XPathQueryServiceImpl) col.getService( "XPathQueryService", "1.0" ); service.setProperty( OutputKeys.INDENT, "yes" ); service.setProperty(OutputKeys.ENCODING, "UTF-8"); ResourceSet result1 = service.query(query1); SAXSerializer serializer = getSAXSerializer(); for(int i=0;i<(int)result1.getSize();i++) { XMLResource res = (XMLResource)result1.getResource(i); ResourceSet result2 = service.query(res, query2); for ( int j = 0; j < (int) result2.getSize(); j++ ) { XMLResource resource = (XMLResource) result2.getResource( (long) j ); resource.getContentAsSAX(serializer); System.out.println(resource.getContent()); } System.out.println("hits: " + result2.getSize()); } releaseSAXSerializer(serializer); } catch ( Exception e ) { e.printStackTrace(); } } public static void main(String args[]) throws Exception { String user = "neotest"; String pwd = "neotest"; String query1 = "collection('/db/ZS_FrontPageBA')/ClinicalDocument/recordTarget/patientRole"; String query2 = "patient/name"; QueryXMLNested(query1,query2,user,pwd); } }