Monday, March 2, 2009

XQuery

Sometimes when u have a big XML document, you want to iterate through some elements of it.  One way of doing it is to parse it and process the needed elements. Another way might be to use XQuery/XPath. You can directly access the needed elements/attributes and their values using XQuery. 
I present an example which uses Saxon s9api. Include saxon and saxon's s9api jar in the classpath. Consider this xml string  -> 
" Abiteboul "

Suppose if we want to know the years of the book (imagine there are 100s of 1000s books in the xml, though this xml shows just 1 book). We can use the follwing java code.

Processor processor = new Processor(false);
ItemTypeFactory itf = new ItemTypeFactory(processor);
ItemType integerType = itf.getAtomicType(new QName("http://www.w3.org/2001/XMLSchema", "integer"));


DocumentBuilder builder = processor.newDocumentBuilder();
        builder.setLineNumbering(true);
        builder.setWhitespaceStrippingPolicy(WhitespaceStrippingPolicy.ALL);
        String str = " Abiteboul ";
        StringValue strVal = new StringValue(str.subSequence(0, str.length()));

        InputStream bais = new ByteArrayInputStream(str.getBytes());
        StreamSource ss = new StreamSource(bais);
XdmNode fileNode = builder.build(ss);

XPathCompiler xpc = processor.newXPathCompiler();
XPathExecutable xqe = xpc.compile("/BOOKS/BOOK/@YEAR");
XPathSelector selector = xqe.load();
selector.setContextItem(fileNode);

XdmValue val = selector.evaluate();
XdmSequenceIterator itr = val.iterator();
while(itr.hasNext()) {
XdmItem xi = itr.next();
XdmNode xn = (XdmNode) xi;
System.out.println(" Node Name " + xn.getNodeName().getLocalName());
System.out.println(" Node Value " + xn.getStringValue());

}


Note - We can read an xml saved in a file too in addition to xml in string variable as in above example.