1、计算阶乘的函数
xquery version "3.0";
declare function local:fact($n as xs:integer) {
if ($n eq 1) then
$n
else
$n * local:fact($n - 1)
,
util:log('warn','n is ' || $n)
};
local:fact(5)
Learn and share.
1、计算阶乘的函数
xquery version "3.0";
declare function local:fact($n as xs:integer) {
if ($n eq 1) then
$n
else
$n * local:fact($n - 1)
,
util:log('warn','n is ' || $n)
};
local:fact(5)
1、QueryFileSOAP.java
package com.neohope.existdb.test;
import org.exist.soap.Query;
import org.exist.soap.QueryResponse;
import org.exist.soap.QueryService;
import org.exist.soap.QueryServiceLocator;
import java.net.URL;
import java.nio.charset.Charset;
public class QueryFileSOAP {
public static void QueryXML(String xquery, String user, String pwd) throws Exception {
QueryService service = new QueryServiceLocator();
Query query = service.getQuery(new URL("http://localhost:8080/exist/services/Query"));
String sessionId = query.connect("neotest", "neotest");
byte[] queryData = xquery.getBytes(Charset.forName("UTF-8"));
QueryResponse resp = query.xquery( sessionId, queryData );
System.out.println( "found: " + resp.getHits() );
if(resp.getHits() == 0) {
return;
}
else {
//get 10 results
byte[][] hits = query.retrieveData(sessionId, 1, 10,
true, false, "elements").getElements();
for (int i = 0; i < hits.length; i++) {
System.out.println(new String(hits[i], "UTF-8"));
}
}
query.disconnect(sessionId);
}
public static void main(String args[]) throws Exception {
String user = "neotest";
String pwd = "neotest";
String query ="for $name in collection('/db/CDA')/ClinicalDocument/recordTarget/patientRole/patient/name \n" +
"return \n" +
"<name>{$name}</name> ";
QueryXML(query, user, pwd);
}
}
1、GetFileSOAP.java
package com.neohope.existdb.test;
import org.exist.soap.Query;
import org.exist.soap.QueryService;
import org.exist.soap.QueryServiceLocator;
import java.net.URL;
public class GetFileSOAP {
public static void GetXML(String fileId, String user, String pwd) throws Exception {
QueryService service = new QueryServiceLocator();
Query query = service.getQuery(new URL("http://localhost:8080/exist/services/Query"));
String session = query.connect(user, pwd);
byte[] data = query.getResourceData(session,
"/db/CDA/"+fileId,
true, false, false);
System.out.println(new String(data, "UTF-8"));
query.disconnect(session);
}
public static void main(String args[]) throws Exception {
String user = "neotest";
String pwd = "neotest";
GetXML("入院患者护理评估单01.xml",user,pwd);
}
}
1、SaveFileSOAP.java
package com.neohope.existdb.test;
import org.exist.soap.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;
import java.nio.charset.Charset;
public class SaveFileSOAP {
public static void SaveXML(String xmlFilePath, String user, String pwd) throws Exception {
AdminService adminService = new AdminServiceLocator();
Admin admin = adminService.getAdmin(new URL("http://localhost:8080/exist/services/Admin"));
String session = admin.connect("neotest", "neotest");
BufferedReader f = new BufferedReader(new FileReader(xmlFilePath));
String line;
StringBuffer xml = new StringBuffer();
while ((line = f.readLine()) != null)
xml.append(line);
f.close();
admin.store(session, xml.toString().getBytes(Charset.forName("UTF-8")), "UTF-8", "/db/CDA/入院患者护理评估单02.xml", true);
admin.disconnect(session);
}
public static void main( String[] args ) throws Exception {
String user = "neotest";
String pwd = "neotest";
SaveXML("PATH_TO_FILE\\入院患者护理评估单02.xml", user, pwd);
}
}
1、QueryFileHTTP.java
package com.neohope.existdb.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
public class QueryFileHTTP {
public static void QueryXML(String query) throws IOException {
URL url = new URL("http://localhost:8080/exist/rest/db/CDA");
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestProperty("Content-Type", "application/xml");
connect.setRequestMethod("POST");
connect.setDoOutput(true);
OutputStream os = connect.getOutputStream();
os.write(query.getBytes(Charset.forName("UTF-8")));
connect.connect();
BufferedReader is = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String line;
while((line = is.readLine()) != null)
System.out.println(line);
}
public static void main(String[] args) throws IOException {
String query ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<query xmlns=\"http://exist.sourceforge.net/NS/exist\" start=\"1\" max=\"10\" cache=\"no\">\n";
query +="<text><![CDATA[\n" +
"for $name01 in /ClinicalDocument/recordTarget/patientRole/patient/name \n" +
"return \n" +
"<name>{$name01}</name> \n" +
"]]></text> \n";
query +="<properties> \n";
query +="<property name=\"indent\" value=\"yes\"/> \n";
query +="<property name=\"encoding\" value=\"UTF-8\"/> \n";
query +="</properties> \n";
query +="</query>";
System.out.println(query);
QueryXML(query);
}
}
1、GetFileHTTP.java
package com.neohope.existdb.test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class GetFileHTTP {
public static void GetXML(String fileId) throws IOException {
URL url = new URL("http://localhost:8080/exist/rest/db/CDA/" + fileId);
System.out.println("GET file from " + url.toString());
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod("GET");
connect.connect();
System.out.println("Result:");
BufferedReader bis = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String line;
while ((line = bis.readLine()) != null) {
System.out.println(line);
}
}
public static void main(String[] args) throws IOException {
GetXML("入院患者护理评估单01.xml");
}
}
1、SaveFileHTTP.java
package com.neohope.existdb.test;
//apache的base64会多一个换行符
//import org.apache.ws.commons.util.Base64;
//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.sun.xml.internal.messaging.saaj.util.Base64;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class SaveFileHTTP {
public static void SaveXML(String inFileName, String fileId) throws IOException {
URL url = new URL("http://localhost:8080/exist/rest/db/CDA/" + fileId);
System.out.println("Save file to " + url.toString());
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod("PUT");
connect.setDoOutput(true);
connect.setRequestProperty("Content-Type", "application/xml");
String userCredentials = "neotest:neotest";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
System.out.println(basicAuth);
connect.setRequestProperty ("Authorization", basicAuth);
File file = new File(inFileName);
InputStream is = new FileInputStream(file);
OutputStream os = connect.getOutputStream();
byte[] buf = new byte[1024];
int c;
while ((c = is.read(buf)) > -1) {
os.write(buf, 0, c);
}
os.flush();
os.close();
System.out.println("Statuscode " + connect.getResponseCode()
+ " (" + connect.getResponseMessage() + ")");
}
public static void main(String[] args) throws IOException {
SaveXML("D:\\MyProjects\\IDEA14\\TestExistDB\\XMLFiles\\医惠\\入院患者护理评估单01.xml", "入院患者护理评估单01.xml");
}
}
1、GetFileRPC.java
package com.neohope.existdb.test;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;
public class GetFileRPC {
public static void GetXML(String documentId, String user, String pwd) throws Exception {
String uri = "http://localhost:8080/exist/xmlrpc";
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(uri));
config.setBasicUserName(user);
config.setBasicPassword(pwd);
client.setConfig(config);
HashMap<String, String> options = new HashMap<String, String>();
options.put("indent", "yes");
options.put("encoding", "UTF-8");
options.put("expand-xincludes", "yes");
options.put("process-xsl-pi", "no");
Vector<Object> params = new Vector<Object>();
params.addElement(documentId);
params.addElement(options);
String xml = (String)
client.execute("getDocumentAsString", params);
System.out.println(xml);
}
public static void GetXMLChuncked(String documentId, String outPath, String user, String pwd) throws IOException, XmlRpcException {
String url = "http://localhost:8080/exist/xmlrpc";
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(url));
config.setBasicUserName(user);
config.setBasicPassword(pwd);
client.setConfig(config);
Hashtable<String, String> options = new Hashtable<String, String>();
options.put("indent", "no");
options.put("encoding", "UTF-8");
Vector<Object> params = new Vector<Object>();
params.addElement(documentId);
params.addElement(options);
FileOutputStream fos = new FileOutputStream(outPath);
HashMap<?, ?> ht = (HashMap<?, ?>) client.execute("getDocumentData", params);
int offset = ((Integer) ht.get("offset")).intValue();
byte[] data = (byte[]) ht.get("data");
String handle = (String) ht.get("handle");
fos.write(data);
while (offset != 0) {
params.clear();
params.addElement(handle);
params.addElement(new Integer(offset));
ht = (HashMap<?, ?>) client.execute("getNextChunk", params);
data = (byte[]) ht.get("data");
offset = ((Integer) ht.get("offset")).intValue();
fos.write(data);
}
fos.close();
}
public static void main(String args[]) throws Exception {
String user = "neotest";
String pwd = "neotest";
//GetXML("/db/CDA/入院患者护理评估单05.xml", user, pwd);
GetXMLChuncked("/db/PNG/兔子.png","兔子1.png", user, pwd);
}
}
1、SaveFileRPC.java
package com.neohope.existdb.test;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.net.URL;
import java.util.Vector;
public class SaveFileRPC {
public static void SaveXML(String xmlFilePath, String user, String pw) throws Exception {
String url = "http://localhost:8080/exist/xmlrpc";
String path = "/db/CDA/入院患者护理评估单05.xml";
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(url));
config.setBasicUserName(user);
config.setBasicPassword(pw);
client.setConfig(config);
BufferedReader f = new BufferedReader(new FileReader(xmlFilePath));
String line;
StringBuffer xml = new StringBuffer();
while ((line = f.readLine()) != null)
xml.append(line);
f.close();
Vector<Object> params = new Vector<Object>();
params.addElement(xml.toString());
params.addElement(path);
params.addElement(new Integer(0));
Boolean result = (Boolean) client.execute("parse", params);
if (result.booleanValue())
System.out.println("document stored.");
else
System.out.println("could not store document.");
}
public static void SaveXMLChuncked(String xmlFilePath, String user, String pwd) throws Exception {
String url = "http://localhost:8080/exist/xmlrpc";
String path = "/db/PNG/兔子.png";
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(url));
config.setBasicUserName(user);
config.setBasicPassword(pwd);
client.setConfig(config);
Vector<Object> params = new Vector<Object>();
String handle = null;
InputStream fis = new FileInputStream(xmlFilePath);
byte[] buf = new byte[4096];
int len;
while ((len = fis.read(buf)) > 0) {
params.clear();
if (handle != null) {
params.addElement(handle);
}
params.addElement(buf);
params.addElement(new Integer(len));
handle = (String) client.execute("upload", params);
}
fis.close();
params.clear();
params.addElement(handle);
params.addElement(path);
params.addElement(new Boolean(true));
params.addElement("image/png");
Boolean result = (Boolean) client.execute("parseLocal", params); // exceptions
if (result.booleanValue())
System.out.println("document stored.");
else
System.out.println("could not store document.");
}
public static void main(String args[]) throws Exception {
String user = "neotest";
String pwd = "neotest";
SaveXML("PATH_TO_FILE\\入院患者护理评估单05.xml", user, pwd);
//SaveXMLChuncked("D:\\MyProjects\\IDEA14\\TestExistDB\\XMLFiles\\兔子.png", user, pwd);
}
}
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);
}
}