Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Better, Faster XML Processing with VTD-XML : Page 5

VTD-XML is a new open source XML processing API that provides a great alternative to SAX and DOM that doesn't force you to trade processing performance for usability. Find out why this Java-based, non-validating parser is faster than DOM and better than SAX.


advertisement
Finishing Up
The final section of the code demonstrates how to incrementally update the content of XML. For the element named "Product" whose "ID" equals "3," the code changes its value to "4." Then it removes the child element "Desc" and writes the updated content into ProductDetailsUpdated.xml.

// incremental update vn.toElement(VTDNav.ROOT); vn.toElement(VTDNav.FIRST_CHILD, "Product"); int i1=0; //vtd index for "3" long l2=0; // offset length for the child element //find the element "Product do { int i = vn.getAttrVal("ID"); if (vn.matchRawTokenString(i,"3")) { i1 = i; vn.toElement(VTDNav.FIRST_CHILD); l2 = vn.getElementFragment(); } } while (vn.toElement(VTDNav.NEXT_SIBLING, "Product")); int tokenOffset = vn.getTokenOffset(i1); int tokenLength = vn.getTokenLength(i1); int fragOffset = (int) l2; int fragLength = (int) (l2 >> 32); File fo = new File("ProductDetailsUpdated.xml"); FileOutputStream fos = new FileOutputStream(fo); // write everything before " fos.write(b, 0, tokenOffset); // overwrite 3 with 4 fos.write("4".getBytes()); // write everything before the element // fragment for "Desc" fos.write(b, tokenOffset+tokenLength, fragOffset-(tokenOffset+tokenLength+1)); // skip the fragment, then write the rest fos.write(b, fragOffset+fragLength +1, b.length - 1 - (fragOffset+fragLength+1));

Here's the content of the final ProductDetailsUpdated.xml file:

<?xml version='1.0'?> <Products> <Product ID="1" Name="Chai" Price="13"> <Desc Value="10 boxes x 20 Bags"/> </Product> <Product ID="2" Name="Chang" Price="10"> <Desc Value="24 -12 oz bottles"/> </Product> <Product ID="4” Name="Tofu" Price="23.25"> </Product> </Products>

You can download the source code for the entire project. VTD-XML is hosted on SourceForge, which has an in-depth discussion on the internals of the algorithm, in case you are curious.



Future Roadmap
The VTD-XML project has plans for a few enhancements in future releases of VTD-XML. For one, the parsed representation of VTD-XML is inherently persistent, meaning that you can save it on disk, or transport it along with the XML file to reduce the overhead of repetitive parsing. A future version will add that capability. Another useful planned feature is implementing XPath, which should further enhance VTD-XML's usability.

To summarize, VTD-XML is a new XML processing API whose primary design goal is to achieve high performance parsing and low memory usage without compromising ease of use. VTD-XML also offers unique incremental update capability. As XML becomes increasingly ubiquitous, its combination of features and attributes together, I feel that VTD-XML has the potential to play a significant role in enabling a range of applications.



Jimmy Zhang is a co-founder of XimpleWare, a provider of high-performance XML processing solutions. He has experience in the fields of electronic design automation and Voice-over IP, having worked for a number of Silicon Valley high-tech companies. He graduated from UC Berkeley with an MS and a BS from the department of EECS.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap