将xquery结果保存到xml文件中

时间:2012-06-22 23:42:20

标签: xpath xquery

我正在使用xqj api在java中实现XQuery,以下是我得到的示例查询,它确实有效。我需要将查询的输出写入.xml文件,任何人都可以帮助我吗?

String queryString =

            "declare variable $docName as xs:string external;" + sep +
            "for $cat in doc($docName)/*/"+ objectName +
             "    where $cat/"+field+" > "+ value +
               " order by $cat/" + field + " "+
            "return $cat";
            XQExpression expression = conn.createExpression();
            expression.bindString(new QName("docName"), filename,
            conn.createAtomicType(XQItemType.XQBASETYPE_STRING));
            XQSequence results = expression.executeQuery(queryString);
            System.out.println(results.getSequenceAsString(new Properties()));

1 个答案:

答案 0 :(得分:1)

试试这个:

XQSequence results = expression.executeQuery(queryString);
FileOutputStream os = new FileOutputStream(new File("output.xml");
results.writeSequence(os, new Properties());