Java Reference
In-Depth Information
Child Nodes of zip are:
#text Node, type is org.apache.crimson.tree.TextNode:
60603
#text Node, type is org.apache.crimson.tree.TextNode:
How It Works
Since we have set the parser configuration in the factory object to include validating the XML, we have
to provide an org.xml.sax.ErrorHandler object for the parser. The TryDOM class implements the
warning() , error() , and fatalError() methods declared by the ErrorHandler interface so an
instance of this class takes care of it.
We call the getDoctype() method for the Document object to obtain the node corresponding to the
DOCTYPE declaration:
DocumentType doctype = xmlDoc.getDoctype(); // Get the DOCTYPE node
System.out.println("DOCTYPE node:\n" + doctype); // and output it
You can see from the output that we only get the class name with a hash code for the object appended.
We will see how we can get more detail a little later.
After outputting a header line showing where the document body starts, we output the contents starting
with the root element. The listNodes() method does all the work. We pass a reference to the root
element that we obtain from the Document object with the statement:
listNodes(xmlDoc.getDocumentElement(),""); // Root element & children
The first argument to listNodes() is the node to be listed and the second argument is the current
indent for output. On each recursive call of the method, we will append a couple of spaces. This will
result in each nested level of nodes being indented in the output by two spaces relative to the parent
node output.
The first step in the listNodes() method is to get the name of the current node by calling its
getNodeName() method:
String nodeName = node.getNodeName(); // Get name of this node
We then output the name of the current node followed by its class name with the statement:
System.out.println(indent + nodeName + " Node, type is "
+ node.getClass().getName()+":");
The indent parameter defines the indentation for the current node. Calling getClass() for the node
object returns a Class object encapsulating its class type. We then call the getName() method for the
Class object to obtain the class type name for the node.
The next statement outputs the node itself:
Search WWH ::




Custom Search