VTD Xml Example

baahu   January 23, 2017   10 Comments on VTD Xml Example

VTD-XML is a good alternative to Simple API for XML (SAX) and Document Object Model (DOM), as it does not force you to trade processing performance for usability. The Java-based, non-validating VTD – XML parser is faster than DOM and better than SAX.Unlike other XML processing technologies, VTD-XML is designed to be capable of random-access without incurring excessive resource overhead.

An important optimization feature of VTD-XML is non-extractive tokenization. Internally, VTD-XML retains the XML message in memory intact and un-decoded, and tokens are represented using starting offset and length exclusively

The Java API of VTD-XML consists of three essential components:

  • VTDGen (VTD generator) that encapsulates the parsing routine that produces the internal parsed representation of XML
  • VTDNav (VTD navigator) which is a cursor-based API that allows for DOM-like random access to the hierarchical structure of XML
  • Autopilot which is the class that allows for document-order element traversal.

The following steps need to be performed to use VTD-XML for processing an XML document either from disk or via HTTP.

  • The first step is to find out the length of the XML document, allocate adequate memory big enough to hold the document, and then read the entire document into memory.
  • The next step is to create an instance of VTDGen and assign the byte array to it using setDoc().
  • The final step is to call parse(boolean ns), to generate the parsed XML representation. When ns is set to true, subsequent document navigation is namespace aware. If parsing succeeds, you can retrieve an instance of VTDNav by calling getNav().

At the onset of navigation, the cursor of the VTDNav instance points at the root element of the XML document. You can use one of the overloaded versions of toElement() function, to move the cursor manually to different positions in the hierarchy. The toElement() function when declared as toElement(int direction) takes an integer as the input, to indicate the direction in which the cursor moves. Defined as class variables of VTDNav, the six possible values of this integer are ROOT, PARENT, FIRST_CHILD, LAST_CHILD, NEXT_SIBLING, and PREV_SIBLING.

Input Xml:

   Googled By God
   Pulkit Ahuja
   Dan Brown
   The Da Vinco Code
   The Magic of Lost Temple


import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import com.ximpleware.AutoPilot;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;

public class vtdbook {

  public static void main(String[] args){

        	HashMap<String,ArticleData> data = new HashMap<String,ArticleData>();
        	File f = new File("/home/mydir/Downloads/book.xml");
            FileInputStream fis = new FileInputStream(f);
            byte[] ba = new byte[(int)f.length()];
            VTDGen vg = new VTDGen();
            VTDNav vn = vg.getNav();

            AutoPilot ap = new AutoPilot(vn);
            //Jump to the section that we want to process
            //Get all the titles and print each of those
            while(ap.evalXPath() != -1)
            	//getText will return the index of  the VTDRecord
            	int titleIndex = vn.getText();
            	//Get the text of the VTDRecord
            	String title = vn.toNormalizedString(titleIndex);
            	System.out.println("Title is "+title);

            //Lets print the ID and the Author(s)
            //Evaluate all the books
            while (ap.evalXPath() != -1) {
              //Get the id of the book
              int idIndex = vn.getAttrVal("id");
              if(idIndex != -1){
            	//We are doing a parseint() since we know id is a integer
                int id = vn.parseInt(idIndex);
                System.out.println("Book id: " + id);
              //Hit to the first element which is author, after that 
              //we will loop to check if there are any further authors
                  int authorIndex = vn.getText();
                  if(authorIndex != -1){
                    String author = vn.toNormalizedString(authorIndex);
                    System.out.println("\tAuthor:" + author);
              //Go back to the parent element
        catch(Exception e)
        	System.out.println("Exception is:"+e.getMessage());



Title is Googled By God
Title is The Da Vinco Code
Title is The Magic of Lost Temple
Book id: 1
	Author:Pulkit Ahuja
Book id: 2
	Author:Dan Brown
Book id: 3

References : https://dzone.com/articles/building-regular-dsls-in-scala

10 thoughts on “VTD Xml Example

  1. Jane

    I’ll right away grasp your rss as I can not to find your e-mail
    subscription hyperlink or e-newsletter service. Do you have
    any? Please let me realize so that I may subscribe. Thanks.

  2. homepage

    Excellent beat ! I wish to apprentie wwhile you amend your web site, how could i subvscribe for
    a blog website? The account aided me a acceptable deal.
    I had been tiny bit acquainted of ths your broadcast proovided
    bright clear idea

  3. slot

    Pretty component to content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your weblog posts.
    Any way I’ll be subscribing to your augment or even I fulfillment you get admission to consistently fast.


Leave a Reply

Your email address will not be published. Required fields are marked *