VTD Xml Example

baahu   January 23, 2017   2 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:

Code:

Output:

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

2 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.

    Reply

Leave a Reply

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