Java Reference
In-Depth Information
Now we can amend main() to call this method:
DocumentType doctype = xmlDoc.getDoctype(); // Get DOCTYPE node
System.out.println("DOCTYPE node:\n" + getDoctypeString(doctype)); // & output it
Here we are replacing the output statement we had previously with this one that calls our new method.
You can try this out with the circle with DTD.xml file or maybe some other XML files with DTD
declarations. At the time of writing, the version of Crimson that is distributed with SDK 1.4 does not
return the internal subset when the getInternalSubset() method is called. If you want to see this
output, try installing the Xerces parser. With Xerces 1.4.2 I got the output:
DOCTYPE node:
<!DOCTYPE circle[
<!ELEMENT circle (position)>
<!ELEMENT position EMPTY>
<!ATTLIST circle
radius CDATA #REQUIRED
>
<!ATTLIST position
x CDATA #REQUIRED
y CDATA #REQUIRED
>
]>
Document body contents are:
circle Node, type is org.apache.xerces.dom.DeferredElementImpl:
[circle: null]
Element Attributes are:
radius=15
Child Nodes of circle are:
#text Node, type is org.apache.xerces.dom.DeferredTextImpl:
[#text:
]
position Node, type is org.apache.xerces.dom.DeferredElementImpl:
[position: null]
Element Attributes are:
x=30
y=50
#text Node, type is org.apache.xerces.dom.DeferredTextImpl:
[#text:
]
You can see that the output from the toString() method for a node is rather different here from that
produced by the Crimson parser.
How It Works
All the work is done in the getDoctypeString() method. It starts out by forming a basic string in a
StringBuffer object:
Search WWH ::




Custom Search