Recently i came across an application where i need to parse as webpage and some xml’s i get from a webservice to fill up my database. I used to do this long back in 2002 but then there was no such standard for parsing such stuff. But this time my life has become easy and we have a Saviour (XQUERY) as i dont need to parse the whole crap manually. We have a friend in form of XQUERY and it helped me. As a Java programmer I tried to find out Java API which can use Query and found out SAXON
Here are snippets of Code how you use Query to get Data from an XML.
Here is sample which we want to parse
<?xml version=”1.0″ encoding=”UTF-8″?>
<result>
<video_template>
<title/>
<genre>
<choice>action</choice>
……………………………
<choice>special</choice>
</genre>
<rating>
<choice>G</choice>
…………………………………
<choice>NC-17</choice>
</rating>
<user_rating>
<choice>1</choice>
…………………………..
<choice>5</choice>
</user_rating>
<summary/>
<details/>
<year/>
<director/>
<studio/>
<runtime/>
<vhs/>
<vhs_stock/>
<dvd/>
<dvd_stock/>
<beta/>
<beta_stock/>
<laserdisk/>
<laserdisk_stock/>
</video_template>
<actors>
<actor id=”0000000c”>Hackman, Gene</actor>
<actor id=”0000003f”>Hamill, Mark</actor>
<actor id=”00000027″>Heames, Darin</actor>
<actor id=”0000001e”>Heche, Anne</actor>
<actor id=”00000009″>Smith, Will</actor>
<actor id=”0000001b” class=”testserws”>Spoonhauer, Lisa</actor>
<actor id=”00000036″ class=”ashwin”>Sylvester, William</actor>
<actor id=”0000002a”>Todd, Antonio</actor>
<actor id=”00000012″>Voight, John</actor>
<actor id=”0000004b”>Woods, James</actor>
<actor id=”1784027567″>Lewis, Daniel</actor>
<actor id=”2096814035″>Manesse, Gaspard</actor>
<actor id=”325442748″>Feito, Raphael</actor>
<actor id=”4231919377″>Morier, Philippe</actor>
<actor id=”2142583927″>Racette, Francine</actor>
</actors>
<videos>
<video id=”id1235AA0″>
<title>The Fugitive</title>
………………………………………….
Now here is XQuery .//actors/actor[@class = ‘ashwin’] to get actor element whose class is ashwin
Here is how you write snippet in java
package xml.tutorial.xquery;
//Java
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import net.sf.saxon.Configuration;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
import org.xml.sax.InputSource;
public class XQueryExample {
public static void main(String[] args) {
// the Q.xquery file
InputStream queryStream = null;
String queryFileName = “getMajors.xql”;
// documentul XML ce va fi interogat este reprezentat de
// fisierul AircraftDealer.xml
File XMLStream = null;
String xmlFileName = “student_collection.xml”;
// Filename to parse
xmlFileName = “videos.xml”;
// File name which contains the Query
queryFileName = “getActors.xql”;
// print the result to the console
OutputStream destStream = System.out;
// compile the XQuery expression
XQueryExpression exp = null;
// create a Configuration object
Configuration C = new Configuration();
// static and dynamic context
StaticQueryContext SQC = new StaticQueryContext(C);
DynamicQueryContext DQC = new DynamicQueryContext(C);
// indentation
Properties props = new Properties();
props.setProperty(OutputKeys.METHOD, “xml”);
props.setProperty(OutputKeys.INDENT, “yes”);
try {
queryStream = new FileInputStream(queryFileName);
SQC.setBaseURI(new File(queryFileName).toURI().toString());
/*
* Compile here and this actually finds if there is any problem in
* Query Formation
*/
exp = SQC.compileQuery(queryStream, null);
SQC = exp.getStaticContext().getUserQueryContext();
} catch (net.sf.saxon.trans.XPathException e) {
System.err.println(e.getMessage());
} catch (java.io.IOException e) {
System.err.println(e.getMessage());
}
// get the XML ready
try {
XMLStream = new File(xmlFileName);
InputSource XMLSource = new InputSource(XMLStream.toURI()
.toString());
SAXSource SAXs = new SAXSource(XMLSource);
DocumentInfo DI = SQC.buildDocument(SAXs);
DQC.setContextNode(DI);
// Here is where you evaluate the Query and Post to Standard Output
exp.run(DQC, new StreamResult(destStream), props);
destStream.close();
} catch (net.sf.saxon.trans.XPathException e) {
System.err.println(e.getMessage());
} catch (java.io.IOException e) {
System.err.println(e.getMessage());
}
}
}
And you will get the following output
<actor id=”00000036″ class=”ashwin”>Sylvester, William</actor>
Incase you require the sample project contact me at ashwin[dot]rayaprolu[@]gmail[dot].com