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..
//Siblings means same level nodes..
/*
* Here
* 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
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
Note the o/p will be C&D not C&D
This is cool... Nikkie, please use this blog for sharing new technical things
ReplyDeleteWhat is the advantage of using VTD Parser instead of SAX, STAX and all ?
ReplyDelete