Node Reporter
import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class NodeReporter extends NodeIterator {
public static void main(String[] args) {
DOMParser parser = new DOMParser();
NodeIterator iterator = new NodeReporter();
for (int i = 0; i < args.length; i++) {
try {
// Read the entire document into memory
parser.parse(args[i]);
Document d = parser.getDocument();
iterator.followNode(d);
}
catch (SAXException e) {
System.err.println(e);
}
catch (IOException e) {
System.err.println(e);
}
}
} // end main
public void processNode(Node node) {
String name = node.getNodeName();
String type = getTypeName(node.getNodeType());
System.out.println("Type " + type + ": " + name);
}
public static String getTypeName(int type) {
switch (type) {
case Node.ELEMENT_NODE: return "Element";
case Node.ATTRIBUTE_NODE: return "Attribute";
case Node.TEXT_NODE: return "Text";
case Node.CDATA_SECTION_NODE: return "CDATA Section";
case Node.ENTITY_REFERENCE_NODE: return "Entity Reference";
case Node.ENTITY_NODE: return "Entity";
case Node.PROCESSING_INSTRUCTION_NODE: return "processing Instruction";
case Node.COMMENT_NODE : return "Comment";
case Node.DOCUMENT_NODE: return "Document";
case Node.DOCUMENT_TYPE_NODE: return "Document Type Declaration";
case Node.DOCUMENT_FRAGMENT_NODE: return "Document Fragment";
case Node.NOTATION_NODE: return "Notation";
default: return "Unknown Type";
}
}
}