I would like to parse my XML string using an XQJ implementation, for example, SAXON. All examples I could find refer to some database connections. Is it possible to use simple String as xml source?
Saxon has an XQJ interface, and you could either use the doc function() from XQuery e.g. :
XQDataSource ds = new SaxonXQDataSource(); XQConnection conn = ds.getConnection(); XQPreparedExpression exp = conn.prepareExpression("doc('file:/some/file.xml')/child::node()"); XQResultSequence result = exp.executeQuery(); while(result.next()) { System.out.println(result.getItemAsString(null)); }
or directly inject in the XML into the query. e.g. -
XQDataSource ds = new SaxonXQDataSource(); XQConnection conn = ds.getConnection(); XQPreparedExpression exp = conn.prepareExpression("<a><b>test</b></a>/child::node()"); XQResultSequence result = exp.executeQuery(); while(result.next()) { System.out.println(result.getItemAsString(null)); }
Try using
void XQExpression.bindDocument(javax.xml.namespace.QName varName, javax.xml.transform.Source value, XQItemType type)
with XQConstants.CONTEXT_ITEM as the first argument, and a StreamSource wrapping a StringReadeer as the second.
Saxon has an XQJ interface, and you could either use the doc function() from XQuery e.g. :
or directly inject in the XML into the query. e.g. -