Konstantin Milyutin
Konstantin Milyutin

Reputation: 12366

String as xml data source in XQJ

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?

Upvotes: 2

Views: 640

Answers (2)

Michael Kay
Michael Kay

Reputation: 163595

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.

Upvotes: 1

adamretter
adamretter

Reputation: 3517

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

Upvotes: 1

Related Questions