- 保存文件
- 取回文件
- 查询
- 嵌套查询
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、SaveFile.java
package com.neohope.existdb.test;
import org.exist.xmldb.XmldbURI;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
import sun.reflect.annotation.ExceptionProxy;
import java.io.File;
public class SaveFile extends Base{
public static void SaveXML(String collectionName, String xmlFilePath, String user, String pwd) throws Exception {
if(cl==null)throw(new ClassNotFoundException(driver));
Database database = (Database)cl.newInstance();
database.setProperty("ssl-enable", "false");
database.setProperty("create-database", "true");
//config.setBasicUserName("guest");
//config.setBasicPassword("guest");
DatabaseManager.registerDatabase(database);
Collection collection = DatabaseManager.getCollection(URI + collectionName,user,pwd);
if(collection == null) {
//create collcetion
Collection root = DatabaseManager.getCollection(URI + XmldbURI.ROOT_COLLECTION,user,pwd);
CollectionManagementService mgtService =
(CollectionManagementService)root.getService("CollectionManagementService", "1.0");
collection = mgtService.createCollection(collectionName.substring((XmldbURI.ROOT_COLLECTION + "/").length()));
}
// create new XMLResource
File f = new File(xmlFilePath);
XMLResource document = (XMLResource)collection.createResource(f.getName(), "XMLResource");
document.setContent(f);
System.out.print("storing document " + document.getId() + "..." + f.getName() + "...");
collection.storeResource(document);
System.out.println("ok.");
}
public static void main(String args[]) throws Exception {
String user = "neotest";
String pwd = "neotest";
SaveXML("/db/ZS_FrontPageBA", "PATH_TO_FILE\\病案首页01.xml",user,pwd);
}
}