Thursday 4 August 2011

Verbose DOM Parser

       
import java.io.FileInputStream;
import java.util.Arrays;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MainClass {
  public static void main(String[] argsthrows Exception {
    boolean namespace = true;
    boolean validating = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(namespace);
    dbf.setValidating(validating);
    DocumentBuilder parser = dbf.newDocumentBuilder();

    FileInputStream fis = new FileInputStream("y.xml");
    Document doc = parser.parse(fis, "y.xml");

    passNode(doc, 0);
  }

  private static void passNode(Node n, int i) {
    println(n.getNodeName() " [" + nodeTypeToString(n.getNodeType()) "]", i);
    printNameValue("Local Name", n.getLocalName(), i + 2);
    printNameValue("Namespace URI", n.getNamespaceURI(), i + 2);
    printNameValue("Prefix", n.getPrefix(), i + 2);
    printNameValue("Value", n.getNodeValue(), i + 2);

    if (n.hasAttributes()) {
      nodeMapWalk("Attributes", n.getAttributes(), i + 2);
    }

    if (n.hasChildNodes()) {
      nodeListWalk("Children", n.getChildNodes(), i + 2);
    }
  }

  private static void println(String s, int i) {
    indent(i);
    System.out.println(s);
  }

  private static void indent(int i) {
    char[] indent = new char[i];
    Arrays.fill(indent, ' ');
    System.out.print(indent);
  }

  private static void printNameValue(String name, String value, int i) {
    if (value != null) {
      println(name + ":" + value, i);
    }
  }

  private static void nodeListWalk(String name, NodeList nl, int i) {
    println(name + ":", i);
    int count = nl.getLength();
    for (int a = 0; a < count; a++) {
      passNode(nl.item(a), i + 2);
    }
  }

  private static void nodeMapWalk(String name, NamedNodeMap nnm, int i) {
    println(name + ":", i);
    int count = nnm.getLength();
    for (int a = 0; a < count; a++) {
      passNode(nnm.item(a), i + 2);
    }
  }

  private static String nodeTypeToString(short n) {
    String nodeType = null;
    switch (n) {
    case Node.ATTRIBUTE_NODE:
      nodeType = "Attribute";
      break;
    case Node.CDATA_SECTION_NODE:
      nodeType = "CDATA Section";
      break;
    case Node.COMMENT_NODE:
      nodeType = "Comment";
      break;
    case Node.DOCUMENT_FRAGMENT_NODE:
      nodeType = "Document Fragment";
      break;
    case Node.DOCUMENT_NODE:
      nodeType = "Document";
      break;
    case Node.DOCUMENT_TYPE_NODE:
      nodeType = "Document Type";
      break;
    case Node.ELEMENT_NODE:
      nodeType = "Element";
      break;
    case Node.ENTITY_NODE:
      nodeType = "Entity";
      break;
    case Node.ENTITY_REFERENCE_NODE:
      nodeType = "Entity.Reference";
      break;
    case Node.NOTATION_NODE:
      nodeType = "Notation";
      break;
    case Node.PROCESSING_INSTRUCTION_NODE:
      nodeType = "Processing Instruction";
      break;
    case Node.TEXT_NODE:
      nodeType = "Text";
      break;
    }
    return nodeType;
  }
}

   
    
    
    
    
    
  

No comments:

Post a Comment