eXistDB简单通讯09(HTTP_REST)

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

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);
    }
}

Leave a Reply

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

*