Visit all nodes from DOM and output node type

 
//Example XML document
/*
An XML Document Containing a Simple Contact List

<?xml version="1.0" standalone="yes"?>

<folks>
   <person>
       <phone>306 555-9999</phone>
       <email>joe@webserver.net</email>
       <name>Wang, Joe</name>
   </person>
   <person>
       <phone>704 555-0000</phone>
       <name>Pet, Rob</name>
       <email>rob@server.com</email>
   </person>
</folks>

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
  static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    DocumentBuilder builder = dbf.newDocumentBuilder();
    InputSource is = new InputSource("xmlFile.xml");
    doc = builder.parse(is);

    TreeDumper td = new TreeDumper();
    td.dump(doc);
  }
}

class TreeDumper {
  public void dump(Document doc) {
    dumpLoop((Node) doc, "");
  }

  private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
      System.out.println(indent + "CDATA_SECTION_NODE");
      break;
    case Node.COMMENT_NODE:
      System.out.println(indent + "COMMENT_NODE");
      break;
    case Node.DOCUMENT_FRAGMENT_NODE:
      System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
      break;
    case Node.DOCUMENT_NODE:
      System.out.println(indent + "DOCUMENT_NODE");
      break;
    case Node.DOCUMENT_TYPE_NODE:
      System.out.println(indent + "DOCUMENT_TYPE_NODE");
      break;
    case Node.ELEMENT_NODE:
      System.out.println(indent + "ELEMENT_NODE");
      break;
    case Node.ENTITY_NODE:
      System.out.println(indent + "ENTITY_NODE");
      break;
    case Node.ENTITY_REFERENCE_NODE:
      System.out.println(indent + "ENTITY_REFERENCE_NODE");
      break;
    case Node.NOTATION_NODE:
      System.out.println(indent + "NOTATION_NODE");
      break;
    case Node.PROCESSING_INSTRUCTION_NODE:
      System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
      break;
    case Node.TEXT_NODE:
      System.out.println(indent + "TEXT_NODE");
      break;
    default:
      System.out.println(indent + "Unknown node");
      break;
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++){
      dumpLoop(list.item(i), indent + "   ");
    }
  }
}
  
Home 
  Java Book 
    Runnable examples  

XML DOM:
  1. Creating XML Element and add to Document
  2. Create and add Attribute to an Element
  3. Parse an XML File with DOM
  4. Parse an XML file with custom error handler
  5. Parse an XML string with DOM and StringReader.
  6. Visit all nodes from DOM
  7. Visit all nodes from DOM and output node type