| By Jimmy Zhang | Article Rating: |
|
| February 20, 2008 02:15 PM EST | Reads: |
31,630 |
Traditionally DOM or SAX-based enterprise applications have to repeat CPU-intensive XML parsing when accessing the same documents multiple times. VTD-XML 2.0 introduces a simple general-purpose XML index called VTD+XML (http://vtd-xml.sourceforge.net/persistence.html) that eliminates the need for repetitive parsing of those applications.
This article combines various examples and the latest benchmark reports to show you how to get started with this indexing. This article also discusses various scenarios and use cases where you may find VTD+XML useful.
Avoid Repetitive XML Parsing with VTD-XML
As discussed in "Simplify XML processing with VTD-XML," to date one of underlying assumptions in XML application development is that an XML document must be parsed before anything else can be done with it. In other words, the processing logic of XML applications can't start without parsing. Frequently considered a threat to database performance, XML parsing is usually many times slower than other XML operations such as XPath evaluation. When those applications perform multiple read-only access to XML data that don't change very often, wouldn't it be nice to able to eliminate the overhead of associated repetitive parsing?
With the native XML indexing feature introduced in version 2.0 of VTD-XML, you can do precisely that. VTDGen, the class encapsulating various parsing routines, now adds "readIndex(...)" and "writeIndex(...)." VTD-XML 2.0 also introduces two new exceptions: indexWriteException and indexReadException.
Let me put those new methods into action and show you how to turn on the indexing capability in your application. Consider the following XML document:
<purchaseOrder orderDate="1999-10-21">
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
</item>
</purchaseOrder>
Below is a simple pre-2.0 VTD-XML code named "printPrice.java" that prints out the content of the element "USPrice." Notice that it parses the XML file and then uses XPath to filter out the target nodes.
import com.ximpleware.*;
import com.ximpleware.xpath.*;
public class printPrice{
public static void main(String args[]){
VTDGen vg = new VTDGen();
try{
if (vg.parseFile("po.xml",true)){
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/purchaseOrder/item/USPrice/text()");
int i=-1;
while((i=ap.evalXPath())!=-1){
System.out.println(" USPrice ==> "+vn.toString(i));
}
}
}catch(Exception e){
}
}
}
A few changes are needed to add VTD-XML's new indexing capability to the Java code above. First, you need to read in the XML document, parse it, and then write out the indexed version of the same XML document. From that point onward, your application can run XPath query or processing logic directly on top of the index, saving the CPU cycles of parsing the XML document again. The following code snippets (named "genIndex.java" and "accessIndex.java" respectively) show you how to generate and access the index. Notice that, when executed sequentially, both applications produce the identical output as "printPrice.java."
The first application (genIndex.java) reads in "po.xml" and produces "po.vxl."
import com.ximpleware.*;
import com.ximpleware.xpath.*;
public class genIndex{
public static void main(String args[]){
VTDGen vg = new VTDGen();
try{
if (vg.parseFile("po.xml",true)){
vg.writeIndex("po.vxl");
}
}catch(Exception e){
}
}
}
The second application (accessIndex.java) loads "po.vxl" and filters the document using XPath expression "/purchaseOrder/item/USPrice/text()."
import com.ximpleware.*;
import com.ximpleware.xpath.*;
public class accessIndex{
public static void main(String args[]){
VTDGen vg = new VTDGen();
try{
VTDNav vn = vg.loadIndex("po.vxl");
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/purchaseOrder/item/USPrice/text()");
int i=-1;
while((i=ap.evalXPath())!=-1){
System.out.println(" USPrice ==> "+vn.toString(i));
}
}catch(Exception e){
}
}
}
Published February 20, 2008 Reads 31,630
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Jimmy Zhang
Jimmy Zhang is a cofounder of XimpleWare, a provider of high performance XML processing solutions. He has working experience in the fields of electronic design automation and Voice over IP for a number of Silicon Valley high-tech companies. He holds both a BS and MS from the department of EECS from U.C. Berkeley.
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Adobe Flex Developer Earns $100K in New York City
- Move Over BI, Here Comes PI - Performance Intelligence
- Yahoo! Query Language
- RothmanResearch.com Market Review and Company Analysis on FULT, DISH, ASBC, CTRP, FSLR and CRBC
- Qt DevDays 2009 - Munich
- The Time Is Right for Enterprise Cloud Computing
- Microsoft Nudges Eclipse Developers to Windows-Ware
- Who Invented Virtualization?
- ExaGrid Sets New Standard in Backup Price, Performance and Capacity with Launch of EX10000E Disk Backup System with Data Deduplication and Expanded 100TB GRID Capacity
- Smearing Cloud Lipstick on a Legacy Tech Pig
- Moving the Operating System & Desktop to the Cloud
- 1st Annual Government IT Conference & Expo: Themes & Topics
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Is Microsoft as Free as Open Source?
- Adobe Flex Developer Earns $100K in New York City
- The Curious Case of Build Release Management eBook
- IBM, Microsoft, Others in Lock-Picking Cloud API Push
- Move Over BI, Here Comes PI - Performance Intelligence
- United Planet offers practical portal building tips for SMBs
- Yahoo! Query Language
- RothmanResearch.com Market Review and Company Analysis on FULT, DISH, ASBC, CTRP, FSLR and CRBC
- Qt DevDays 2009 - Munich
- The Time Is Right for Enterprise Cloud Computing
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- The Top 250 Players in the Cloud Computing Ecosystem
- Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
- Ulitzer to Give Drupal 6.0 Its Biggest Scalability Challenge Yet
- An Introduction to Ant
- Appcelerator Named "Platinum Sponsor" of AJAX World Conference & Expo
- "What's New and Exciting About the Web Right Now?" Asks Time Magazine
- Oracle To Keynote Cloud Computing Expo
- First Eclipse Project Targeting PHP Now Available
- AJAX World - Two Great PDF Creators
- C#, Turbo Pascal, C++, PHP...and the LEGO Brick: Denmark's Leading Exports
- Rolling Your Own MVC: The Page Load Scenario




























