Improve XPath Efficiency with VTD-XML : Page 3

Even though XML and XPath have been around for several years, there's still room for performance improvementsand VTD-XML and its XPath implementation provide them.

by Jimmy Zhang

Mar 19, 2007

Page 3 of 4

Querying Multiple Documents
Another common scenario consists of executing the same XPath query against multiple XML documents. In this case, you can reuse the XPath query for each document, through AutoPilot's delayed binding feature. Here's the procedure: After executing a query on one XML document, you bind (by calling bind(...)) the same AutoPilot object to a new VTDNav object that corresponds to a different XML document. You also need to call resetXPath(), which resets the internal XPath state so it is ready for reuse.
Listing 2 shows an example that applies two XPath expressions to two different XML documents.

The Benefits
When comparing with other XPath engines, VTD-XML's non-blocking XPath evaluation approach has a number of benefits. Without sacrificing any inherent benefits of XPath, VTD-XML offers unsurpassed flexibility and puts more control in your hands. Consider the use case in which you want to use XPath to process a large XML document and the node set contains thousands of nodes. With other XPath engines, your application logic will have to wait for the entire node set to be returned before beginning to process matches. But with VTD-XML, your application logic can begin processing matches as soon as the XPath implementation finds and returns the first node.
If your application processes only a small number of nodes in the node set, or you have prior knowledge of the XML document (e.g. you know there is only one occurrence of the matching node at the beginning of a large XML file), this saves the time you'd have to wait while the XPath implementation finishes searching the entire document. In this case, other types of XPath evaluation unnecessarily block the program flow and seem more like the wrong approach!

As another example, you can use VTD-XML's non-blocking XPath to walk the nodes, which simply isn't possible with other types of XPath engines.
When combined with the other benefits and features of VTD-XML, the comparison with DOM and SAX gets lopsided in a hurry. Regardless of the file sizes, whether it is parsing, updating or XPath evaluation (shown later), VTD-XML delivers the kind of across-the-board performance improvements never before thought possible by many. For transactional SOA applications dealing with small-to-medium-size XML files, VTD-XML easily blows away DOMand the benefit increases for large XML files. When compared with SAX, you literally move from long, ugly, and difficult-to-maintain SAX code to clean, short VTD-XML code, while enjoying the full power of VTD-XML's random access and improving performance between 10 and 50 times along the way.

If you regularly deal with large XML documents or XPath expressions returning huge node sets, VTD-XML is a must-have!