Show TOC

 TraversingLocate this document in the navigation structure

Use

If you want to access an XML document directly, you can traverse the tree structure that represents the XML document.

The basic classes for traversing a document are:

  • org.w3c.dom.Node
  • org.w3c.dom.Element
  • org.w3c.dom.Text
  • org.w3c.dom.Comment
  • org.w3c.dom.Document
  • org.w3c.dom.NodeList
  • org.w3c.dom.NamedNodeMap
Example

The following examples clarify the process of traversing a tree structure:

Tip
public void traverse1(Node node) {
    do {
       System.out.println("Node: name=" + node.getNodeName() +
       ", value=" + node.getNodeValue() + ", type=" +
       node.getNodeType());
       if (node.getFirstChild() != null) {
        System.out.println("Processing children:");
        traverse1(node.getFirstChild());
      }
    }  while ((node = node.getNextSibling()) != null);
  }
Note
public void traverse2(Node node) {
    //Get the children of this Node
      NodeList children = node.getChildNodes();
   
      //go through all the children of the node
      for (int i=0; i<children.getLength(); i++) {
         //get the next child
         Node child = children.item(i);
         //get the type of the child
         short childType = child.getNodeType();
         if (childType == Node.ELEMENT_NODE) {
        //if the child is an Element then print the start and end
        //tags and recurse the content       
        String nodeName = child.getNodeName();
        System.out.print("<" + nodeName + ">");
        traverse2(child);
        System.out.print("</" + nodeName + ">");
      } else if (childType == Node.TEXT_NODE) {
        //if the child is a Text node just print the text content
        String data = child.getNodeValue();
        System.out.print(data);
    }
    }
  }
Note
public void traverse3(Node node, int indent) {
    for (int i = 0; i < indent; i++) {
      System.out.print("   ");
    }
    int type = node.getNodeType();
    switch (type) {
    case Node.ATTRIBUTE_NODE:
      System.out.println("ATTRIBUTE_NODE");
      break;
    case Node.CDATA_SECTION_NODE:
      System.out.println("CDATA_SECTION_NODE");
      break;
    case Node.COMMENT_NODE:
      System.out.println("COMMENT_NODE");
      break;
    case Node.DOCUMENT_FRAGMENT_NODE:
      System.out.println("DOCUMENT_FRAGMENT_NODE");
      break;
    case Node.DOCUMENT_NODE:
      System.out.println("DOCUMENT_NODE");
      break;
    case Node.DOCUMENT_TYPE_NODE:
      System.out.println("DOCUMENT_TYPE_NODE");
      break;
    case Node.ELEMENT_NODE:
      System.out.println("ELEMENT_NODE");
      NamedNodeMap atts = node.getAttributes();
      for (int i = 0; i < atts.getLength(); i++) {
        Node att = atts.item(i);
        traverse3(att, indent + 1);
      }
      break;
    case Node.ENTITY_NODE:
      System.out.println("ENTITY_NODE");
      break;
    case Node.ENTITY_REFERENCE_NODE:
      System.out.println("ENTITY_REFERENCE_NODE");
      break;
    case Node.NOTATION_NODE:
      System.out.println("NOTATION_NODE");
      break;
    case Node.PROCESSING_INSTRUCTION_NODE:
      System.out.println("PROCESSING_INSTRUCTION_NODE");
      break;
    case Node.TEXT_NODE:
      System.out.println("TEXT");
      break;
    default:
      System.out.println("???");
      break;
    }
    for (Node c = node.getFirstChild(); c != null; c =
    c.getNextSibling()) {
      traverse3(c, indent + 1);
    }
  }