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
int i1=0; //vtd index for "3"
long l2=0; // offset length for the child element
//find the element "Product
int i = vn.getAttrVal("ID");
i1 = i;
l2 = vn.getElementFragment();
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
// write everything before the element
// fragment for "Desc"
// 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
<Product ID="1" Name="Chai"
<Desc Value="10 boxes x 20 Bags"/>
<Product ID="2" Name="Chang"
<Desc Value="24 -12 oz bottles"/>
<Product ID="4” Name="Tofu"
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.
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.