July 20, 2011

Parsing XML with a DOM Parser

The DOM Parser in the example code below is a validating parser. The parser code, processes the element, attribute and the text nodes of the document and prints them to the console (while ignoring the rest of the XML artifacts). It does not modify the XML document eventhough it maintains an in-memory representation of the XML Document.

package info.icontraining.parsers;

import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMParserWithDTD implements ErrorHandler {
 
   public static void main(String[] args) throws SAXException, IOException  {
  
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  
      try {
         factory.setNamespaceAware(true);
         factory.setValidating(true);
         factory.setFeature("http://apache.org/xml/features/validation/schema", true);
         factory.setIgnoringElementContentWhitespace(true);
   
         DocumentBuilder dom = factory.newDocumentBuilder();
         dom.setErrorHandler(new TestDOMParser());
         Document doc = dom.parse("Invoice.xml");
   
         processNode(doc);

      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      }     
   } 
   
   private static void processNode(Node node) {

      NodeList list = node.getChildNodes();   
      for(int i=0; i<list.getLength(); i++) {
 
         if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
            processElementNode(list.item(i));
            processNode(list.item(i));
         }
   
         if (list.item(i).getNodeType() == Node.TEXT_NODE) {
            processTextNode(list.item(i));
         }
      }
   }
 
   private static void processTextNode(Node text) {  
      if (text.getNodeValue().trim().length() != 0)
         System.out.println(text.getNodeValue().trim());
   }    

   private static void processElementNode(Node element) {
  
      String temp = "";
      temp = temp + "<" + element.getNodeName();
  
      if (element.hasAttributes()) {
         NamedNodeMap map = element.getAttributes();
        
         for(int i=0; i<map.getLength(); i++) {
            temp = temp + " " + map.item(i).getNodeName();
            temp = temp + "=\"" + map.item(i).getNodeValue() + "\"";
         }
      }
  
      temp = temp + ">";
      System.out.println(temp);
   }
 
   public void fatalError(SAXParseException err) throws SAXException {

      System.out.println("** Fatal Error" 
                          + ", line " + err.getLineNumber() 
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }     

   public void error(SAXParseException err) throws SAXParseException {
      System.out.println("** Error" 
                          + ", line " + err.getLineNumber() 
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }

   public void warning(SAXParseException err) throws SAXParseException {
      System.out.println("** Warning" 
                          + ", line " + err.getLineNumber()              
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }
}

No comments:

Post a Comment