使用saxon将XdmValue作为XmlType存储在数据库中的更好方法。

时间:2015-05-21 06:47:10

标签: java oracle saxon xmltype

我正在使用Saxon 9API执行XQuery。 XQuery的结果以net.sf.saxon.s9api.XdmValue的形式返回。我正在从这个XdmValue构造DOM文档对象。我正在粘贴下面的代码。

    Processor saxon = new Processor(false);
    saxon.registerExtensionFunction(new MyExtension());

    XQueryCompiler compiler = saxon.newXQueryCompiler();
    XQueryExecutable exec = compiler.compile(new File("input/studentXQuery.xq"));
    XQueryEvaluator query = exec.load();


    String students = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><student_list><student><name>George Washington</name><major>Politics</major><phone>312-123-4567</phone><email>gw@example.edu</email></student><student><name>Janet Jones</name><major>Undeclared</major><phone>311-122-2233</phone><email>janetj@example.edu</email></student><student><name>Joe Taylor</name><major>Engineering</major><phone>211-111-2333</phone><email>joe@example.edu</email></student></student_list>";
    javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(students));
    Document doc1 = db.parse(is);


    DocumentBuilder builder = saxon.newDocumentBuilder();
    Source src = new DOMSource(doc1);
    XdmNode doc = builder.build(src);
    query.setExternalVariable(new QName("student_list"), doc);

    XdmValue result = query.evaluate();

    System.out.println("Result is -------- "+result.toString());
    InputSource is1 = new InputSource();
    is1.setCharacterStream(new StringReader(result.toString()));
    Document resultDoc = db.parse(is1);
    System.out.println("Result doc is "+resultDoc);

我的问题是在Oracle DB中将此输出存储为XmlType的更好方法是什么。是否需要DOM文档构造来避免处理字符串以保存xml值?或者是在String本身之上构造XmlType的好方法。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

使用DOM作为中间形式似乎非常笨重。我怀疑将序列化Saxon结果并从字符串中重新分析它同样容易/快速 - 但我不知道Oracle提供了哪些选项。