XML Tree Dumper 2 : DOM Edit « XML « Java






XML Tree Dumper 2

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

<?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 java.io.IOException;

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

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Entity;
import org.w3c.dom.EntityReference;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Notation;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

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

  private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
      dumpAttributeNode((Attr) node, indent);
      break;
    case Node.CDATA_SECTION_NODE:
      dumpCDATASectionNode((CDATASection) node, indent);
      break;
    case Node.COMMENT_NODE:
      dumpCommentNode((Comment) node, indent);
      break;
    case Node.DOCUMENT_NODE:
      dumpDocument((Document) node, indent);
      break;
    case Node.DOCUMENT_FRAGMENT_NODE:
      dumpDocumentFragment((DocumentFragment) node, indent);
      break;
    case Node.DOCUMENT_TYPE_NODE:
      dumpDocumentType((DocumentType) node, indent);
      break;
    case Node.ELEMENT_NODE:
      dumpElement((Element) node, indent);
      break;
    case Node.ENTITY_NODE:
      dumpEntityNode((Entity) node, indent);
      break;
    case Node.ENTITY_REFERENCE_NODE:
      dumpEntityReferenceNode((EntityReference) node, indent);
      break;
    case Node.NOTATION_NODE:
      dumpNotationNode((Notation) node, indent);
      break;
    case Node.PROCESSING_INSTRUCTION_NODE:
      dumpProcessingInstructionNode((ProcessingInstruction) node, indent);
      break;
    case Node.TEXT_NODE:
      dumpTextNode((Text) node, indent);
      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 + "   ");
  }

  /* Display the contents of a ATTRIBUTE_NODE */
  private void dumpAttributeNode(Attr node, String indent) {
    System.out.println(indent + "ATTRIBUTE " + node.getName() + "=\""
        + node.getValue() + "\"");
  }

  /* Display the contents of a CDATA_SECTION_NODE */
  private void dumpCDATASectionNode(CDATASection node, String indent) {
    System.out.println(indent + "CDATA SECTION length=" + node.getLength());
    System.out.println(indent + "\"" + node.getData() + "\"");
  }

  /* Display the contents of a COMMENT_NODE */
  private void dumpCommentNode(Comment node, String indent) {
    System.out.println(indent + "COMMENT length=" + node.getLength());
    System.out.println(indent + "  " + node.getData());
  }

  /* Display the contents of a DOCUMENT_NODE */
  private void dumpDocument(Document node, String indent) {
    System.out.println(indent + "DOCUMENT");
  }

  /* Display the contents of a DOCUMENT_FRAGMENT_NODE */
  private void dumpDocumentFragment(DocumentFragment node, String indent) {
    System.out.println(indent + "DOCUMENT FRAGMENT");
  }

  /* Display the contents of a DOCUMENT_TYPE_NODE */
  private void dumpDocumentType(DocumentType node, String indent) {
    System.out.println(indent + "DOCUMENT_TYPE: " + node.getName());
    if (node.getPublicId() != null)
      System.out.println(indent + " Public ID: " + node.getPublicId());
    if (node.getSystemId() != null)
      System.out.println(indent + " System ID: " + node.getSystemId());
    NamedNodeMap entities = node.getEntities();
    if (entities.getLength() > 0) {
      for (int i = 0; i < entities.getLength(); i++) {
        dumpLoop(entities.item(i), indent + "  ");
      }
    }
    NamedNodeMap notations = node.getNotations();
    if (notations.getLength() > 0) {
      for (int i = 0; i < notations.getLength(); i++)
        dumpLoop(notations.item(i), indent + "  ");
    }
  }

  /* Display the contents of a ELEMENT_NODE */
  private void dumpElement(Element node, String indent) {
    System.out.println(indent + "ELEMENT: " + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++)
      dumpLoop(nm.item(i), indent + "  ");
  }

  /* Display the contents of a ENTITY_NODE */
  private void dumpEntityNode(Entity node, String indent) {
    System.out.println(indent + "ENTITY: " + node.getNodeName());
  }

  /* Display the contents of a ENTITY_REFERENCE_NODE */
  private void dumpEntityReferenceNode(EntityReference node, String indent) {
    System.out.println(indent + "ENTITY REFERENCE: " + node.getNodeName());
  }

  /* Display the contents of a NOTATION_NODE */
  private void dumpNotationNode(Notation node, String indent) {
    System.out.println(indent + "NOTATION");
    System.out.print(indent + "  " + node.getNodeName() + "=");
    if (node.getPublicId() != null)
      System.out.println(node.getPublicId());
    else
      System.out.println(node.getSystemId());
  }

  /* Display the contents of a PROCESSING_INSTRUCTION_NODE */
  private void dumpProcessingInstructionNode(ProcessingInstruction node,
      String indent) {
    System.out.println(indent + "PI: target=" + node.getTarget());
    System.out.println(indent + "  " + node.getData());
  }

  /* Display the contents of a TEXT_NODE */
  private void dumpTextNode(Text node, String indent) {
    System.out.println(indent + "TEXT length=" + node.getLength());
    System.out.println(indent + "  " + node.getData());
  }

  static public void main(String[] arg) {
    String filename = null;
    boolean validate = false;

    if (arg.length == 1) {
      filename = arg[0];
    } else if (arg.length == 2) {
      if (!arg[0].equals("-v"))
        usage();
      validate = true;
      filename = arg[1];
    } else {
      usage();
    }

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    // Parse the input to produce a parse tree with its root
    // in the form of a Document object
    Document doc = null;
    try {
      DocumentBuilder builder = dbf.newDocumentBuilder();
      builder.setErrorHandler(new MyErrorHandler());
      InputSource is = new InputSource(filename);
      doc = builder.parse(is);
    } catch (SAXException e) {
      System.exit(1);
    } catch (ParserConfigurationException e) {
      System.err.println(e);
      System.exit(1);
    } catch (IOException e) {
      System.err.println(e);
      System.exit(1);
    }

    // Use a TreeDumper to list the tree
    TreeDumper2 td = new TreeDumper2();
    td.dump(doc);
  }

  private static void usage() {
    System.err.println("Usage: DOMCheck [-v] <filename>");
    System.exit(1);
  }
}

class MyErrorHandler implements ErrorHandler {
  public void warning(SAXParseException e) throws SAXException {
    show("Warning", e);
    throw (e);
  }

  public void error(SAXParseException e) throws SAXException {
    show("Error", e);
    throw (e);
  }

  public void fatalError(SAXParseException e) throws SAXException {
    show("Fatal Error", e);
    throw (e);
  }

  private void show(String type, SAXParseException e) {
    System.out.println(type + ": " + e.getMessage());
    System.out.println("Line " + e.getLineNumber() + " Column "
        + e.getColumnNumber());
    System.out.println("System ID: " + e.getSystemId());
  }
}

           
         
    
    
    
    
    
  








Related examples in the same category

1.Creates element node, attribute node, comment node, processing instruction and a CDATA section
2.Java DOM edit: Locate a Node and Change Its Content
3.Java DOM edit: Locating a Node by Using Siblings
4.Java DOM edit: Delete the First Child of the Root Node
5.Java DOM edit: Replacing an Existing Node with a New One
6.Java DOM edit: Add an Element Containing All Names
7.Java DOM edit: Duplicate a Subtree
8.Java DOM edit: Adding an Attribute to an Element
9.Java DOM edit: Deleting Two Attributes
10.Java DOM edit: Copying Attributes
11.Java DOM edit: A Method to Find an ID Value and Print the Element Text
12.Java DOM edit: Modifying Text by Replacement
13.Java DOM edit: Modifying Text by Cutting and Pasting
14.Java DOM edit: Edit Text by Insertion and Replacement
15.Java DOM edit: Replacing a Text Node with a New CDATASection Node
16.Java DOM edit: Splitting One Text Node into Three
17.Java DOM edit: Normalize All of the Text in a Document
18.Java DOM edit: Creates a New DOM Parse Tree
19.Java DOM edit: Copy a Node from One Parse Tree into Another
20.Java DOM edit: Creating a DocumentFragment Subtree and Appending to the Document
21.Java DOM edit: Insert a Processing Instruction and a Comment
22.A Method for Inserting a New Entry in a List
23.Add a comment at the beginning of the document
24.Change a particular node in XML
25.Create a new element and move the middle text node to it
26.Insert the new element where the middle node used to be
27.Set text in a Node
28.Copies the source tree into the specified place in a destination tree.
29.Copy a Node from one source document
30.Generates a DOM from scratch. Writes the DOM to a String using an LSSerializer.
31.Create Element With Text