dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Better, Faster XML Processing with VTD-XML : Page 4

VTD-XML is a new open source XML processing API that provides a great alternative to SAX and DOM that doesn't force you to trade processing performance for usability. Find out why this Java-based, non-validating parser is faster than DOM and better than SAX.


advertisement
A Sample Project
So far, you have had a high-level overview of VTD-XML. Now it's time to put it into action and demonstrate some of its important concepts and features. The following examples use a ProductDetails.xml file, whose content is shown below.

 <?xml version='1.0'?>
  <Products>
     <Product ID="1" Name="Chai" 
       Price="13">
       <Desc Value="10 boxes x 20 Bags"/>        
     </Product>
     <Product ID="2" Name="Chang" 
       Price="10">      
       <Desc Value="24 -12 oz bottles"/>      
     </Product>
     <Product ID="3" Name="Tofu" 
       Price="23.25">
       <Desc Value="40-100 kg pkgs"/>   
     </Product>
   </Products>
The sample code parses the file using VTDGen, navigates the hierarchy using VTDNav, and then performs a simple element node iteration using AutoPilot. Also you will see how to incrementally update the content of this XML file.

For this project, create a Java class named project1.java, which only has a main() method. To handle all possible exceptions, the code uses a single try/catch block wrapped around the application logic.


try {
       // application logic goes here
   }
   catch (ParseException e){
     System.out.println(
        " XML file parsing error \n"+e);
   }
   catch (NavException e){
     System.out.println(
        " Exception during navigation "+e);
   }
   catch (java.io.IOException e)
   {
     System.out.println(" IO exception condition"+e);
   }   
The following code snippet reads the file content into a buffer, and then generates a parsed representation for ProductDetails.xml.

 // parse XML
   File f = new File("ProductDetails.xml"); 
   FileInputStream fis =  new FileInputStream(f);

   // find out file length and allocate buffer
   byte[] b = new byte[(int) f.length()]; 

   // read file content into the buffer
   fis.read(b);    

   //create an instance of VTDGen
   VTDGen vg = new VTDGen(); 

   // assign the buffer to VTDGen
   vg.setDoc(b); 

   // parse with namespace awareness turned off
   vg.parse(false); 
Next, it obtains an instance of VTDNav from VTDGen and moves the cursor to a child element named product whose id attribute equals "3."

   // manual navigation

   // get the navigator
   VTDNav vn = vg.getNav();      

   // verify the root element name
   if (vn.matchElement("Products")){         

      // move the cursor to first child 
      // named "product"
      if (vn.toElement(
         VTDNav.FIRST_CHILD,"Product")) {
         // stepping across every sibling 
         // named "Product"
         do {
            int i = vn.getAttrVal("ID");
            if (vn.matchRawTokenString(i,"3")) {
               System.out.println("element name ==>" + 
                  vn.toString(vn.getCurrentIndex()));
               System.out.println(
                  "element fragement is ==>");
               // then print out element fragment 
               // using getElementFragment();
               long l = vn.getElementFragment(); 
               int len = (int) (l>>32);
               int offset = (int) l;
               System.out.println(new String(
                  b,offset, len));
            }
         }
         while(vn.toElement(
            VTDNav.NEXT_SIBLING,"Product"));
      }
      else 
         System.out.println("no child named Product");
   }
The output from executing the code snippet above is shown below.

 element name ==> Product
element fragment is ==>
   <Product ID="3" Name="Tofu"
   Price="23.25">
   <Desc Value="40-100 kg pkgs"/>
</Product>
Next, the cursor gets reset to the root, then the code creates an instance of AutoPilot to iterate through all elements of the name "Desc."

// iterate over element using AutoPilot
   vn.toElement(VTDNav.ROOT);
   AutoPilot ap = new AutoPilot(vn);
   ap.selectElement("Desc"); //select "Desc"
   while(ap.iterate())
   {
      long l = vn.getElementFragment();
      int len = (int) (l>>32);
      int offset = (int) l;
      System.out.println(new String(b,offset,len));
   }
And its corresponding output looks like this:

<Desc Value="10 boxes x 20 Bags"/>
<Desc Value="24 -12 oz bottles"/>
<Desc Value="40-100 kg pkgs"/>


Thanks for your registration, follow us on our social networks to keep up-to-date