DOM Based XML Parsing


// DOM Based XML Parsing

import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

 

class DOMParser

{

     static public void main(String args[])

     {

          try

          {

              DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

             

              DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();

 

              Document document = parser.parse(new File(args[0]));

 

              NodeList links = document.getElementsByTagName("link");

             

              System.out.println("Input document has " + links.getLength() + " 'link' elements.");

 

              NodeList ch = document.getElementsByTagName("channel");

              System.out.println("Input document has " + ch.getLength() + " 'channel' elements.");

 

              NodeList desc = document.getElementsByTagName("description");

              System.out.println("Input document has " + desc.getLength() + " 'description' elements.");

          }

 

          catch (Exception e)

          {

              e.printStackTrace();

          }

          return;

     }

}

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu