Thursday, July 16, 2009

VTD Parser First Look

I had beeen hearing about VTD parser a long way back but got the opportunity to look into it today only.What I heared about VTD was that it is used in applications which demand very high performance requirements.From my friend Jijo who is pretty experinced in using VTd ,says that the attractive thing about VTD is that its API is simple & easy to learn.What he menat was there are only a very few classess & functions we need to remember to get things done.Also pne emore condition is like the XML document template should be exactly same each time we parse it.No change is allowed in the document structure as we are writing the code based on the structure of the XML document to an extent.So with out talking so much we can move on to the implementation.
I created a Java project in Eclipse , you have to add the VTD-XML.jar to the class path.No I wrote a small Library XML file which can be used as input for parsing..
The sample input file is shown in the top left corner...

Let me write some XML basics..

//Siblings means same level nodes..
/*
* Here tags are siblings & , ... are also siblings as
* the came under the same level.
*
* */
With this understanding we can move on to the code..

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

public class SimpleXMLParser {
public static void main(String[] args) {
VTDGen vg = new VTDGen();
AutoPilot ap=null;
if (vg.parseFile("E:\\ReporteCLIPSEwORKsPACE\\VTDWork\\src\\MyInput.xml",true)){// set namespace awareness to true
VTDNav vn = vg.getNav();//Some thing like having a file pointer
try {

//toElement is something that we use for navigation
vn.toElement(vn.FIRST_CHILD);//Move to Books
System.out.println(vn.toString(vn.getAttrVal("count")));//Printing attributes using getAttrVal()
vn.toElement(vn.FIRST_CHILD);//Move to name
System.out.println(vn.toString(vn.getText()));//Printing tag values using getText()
vn.toElement(vn.NEXT_SIBLING);
System.out.println(vn.toString(vn.getText()));
vn.toElement(vn.NEXT_SIBLING);
vn.toElement(vn.NEXT_SIBLING);
vn.toElement(vn.NEXT_SIBLING);
System.out.println(vn.toString(vn.getText()));
vn.toElement(vn.PARENT);
vn.toElement(vn.NEXT_SIBLING);
vn.toElement(vn.FIRST_CHILD);
System.out.println(vn.toString(vn.getText()));
//Again move back to root for fun
vn.toElement(vn.ROOT);
//You can use an AutoPilot to traverse though the XML document
//based on a particular tag.
ap = new AutoPilot(vn);
ap.bind(vn);

//No we will iterate based on the tag in the XML document...
ap.selectElement("Books");
int count=0;//initialise
while(ap.iterate()){
count++;
}
System.out.println("Count "+count);
} catch (NavException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("Failed..");
}
}
}
Output:

15
C++
14555C&D5666
2555
Java
Count 2

Just to show u how to use ampersand(&) in XML i had included & in the
14555C&D5666 tag..

Note the o/p will be C&D not C&D