X Query and XPath


Optimization in XML Parsing through X Query and XPath 

Xquery is XML Query language for query xml data. 
You can find more about it http://www.w3schools.com/
Here are some points when we are processing xml data. If you have 1 KB Data Xml, It is easy to parse, but if you have 1 MB Data Xml or bigger then that it take lot of time in parsing. In this scenario XQuery help us for parsing and reduce processing time. How it works?
XML File
StringBuffer xmlSB = new StringBuffer();
xmlSB.append(" ");
xmlSB.append("");
xmlSB.append(" ");
xmlSB.append("");
xmlSB.append(" 900 Aurora Ave.");
xmlSB.append(" Seattle");
xmlSB.append(" LA");
xmlSB.append(" 98115");
xmlSB.append(" ");
xmlSB.append("
");
xmlSB.append(" 2011 152nd Avenue NE");
xmlSB.append(" Redmond");
xmlSB.append(" FL");
xmlSB.append(" 98052");
xmlSB.append(" ");
xmlSB.append(" (425)555-5665");
xmlSB.append(" (206)555-5555");
xmlSB.append(" (206)555-4351");
xmlSB.append(" ");
xmlSB.append(" ");
xmlSB.append("
");
xmlSB.append(" 900 Aurora Ave.");
xmlSB.append(" Seattle");
xmlSB.append(" WA");
xmlSB.append(" 420");
xmlSB.append(" ");
xmlSB.append("
");
xmlSB.append(" 2011 152nd Avenue NE");
xmlSB.append(" Redmond");
xmlSB.append(" WA");
xmlSB.append(" 98052");
xmlSB.append(" ");
xmlSB.append(" (425)555-5665");
xmlSB.append(" (206)555-5555");
xmlSB.append(" (206)555-4321");
xmlSB.append(" ");
xmlSB.append("");

Java Code:- We have two different approaches to query xml data.
File xml = xmlFile;// xmlFile is reference of xml file.
XmlObject xmlObject = XmlObject.Factory.parse( xml );
//If Predefined namespace
StringBuffer xQuerySB = new StringBuffer();
xQuerySB.append( " declare namespace xq='http://www.w3.org/2001/XMLSchema'; " );
xQuerySB.append( " for $e in $this/xq:catalog/xq:journal " );
xQuerySB.append( " return $e/journal[@publisher='apress'] " );
XmlObject[] results = xmlObject.execQuery( xQuerySB.toString() );

(1)Execute Query by XmlObject
If Setting Default namespace
xmlOptions.setUseDefaultNamespace();
XmlObject[] results = xmlObject.execQuery(xQuerySB.toString(), xmlOptions );
If setting multiple namespace
XmlOptions xmlOptions = new XmlOptions();
Map prefixnameSpacehashMap = new hashMap();//If we have multiple schema.
prefixnameSpacehashMap.put(“xq”,” http://www.w3.org/2001/XMLSchema”);
xmlOptions.setLoadAdditionalNamespaces(prefixnameSpacehashMap )
XmlObject[] results = xmlObject.execQuery(xQuerySB.toString(),, xmlOptions );

(2)Execute Query by XmlCursor
 XmlCursor xmlCursor = xmlObject.newCursor();//Creating new Cursor
StringBuffer xQuerySB = new StringBuffer();
xQuerySB.append( " for $a in $this/catalog/journal " );
xQuerySB.append( " return $a/article[@section='Java Technology'] " );
XmlCursor resultCursor = xmlCursor.execQuery( xQuerySB.toString() );
XmlCursor is useful to get any value from xml.
xmlCursor.selectPath( "$this/catalog/journal/@publisher" );
while( xmlCursor.toNextSelection() )
{
System.out.println( xmlCursor.getTextValue() );
}

You can parse xml conditionally by using “xmlCursor.selectPath”
References: http://www.w3.org/2001/XMLSchema, http://xmlbeans.apache.org/




Comments

Popular Posts