Download lang.dtd (4 KB ZIP) here.
Chapter 8 examples do not work. I believe I downloaded and installed the software correctly. How can I get these to run?
The following solution applies to all examples in XML How to Program that use method XMLDocument.write to output XML documents. The latest XML APIs are included in the Java 2 Software Development Kit, version 1.4.1.
The Java examples in XML How to Program are based on Sun's Java API for XML Parsing (JAXP) version 1.0.1, which was current as of this book's writing. Unfortunately, when Sun released the newer JAXP 1.2 they removed the previous versions from their Web site.
JAXP uses a so-called pluggability layer for XML parsers that allows developers to use the JAXP with any JAXP-compliant XML parser. Basically, JAXP provides a set of interfaces that define the requirements for an XML parser. Individual parser vendors build concrete implementations of the JAXP interfaces to provide a JAXP-compliant parser. Developers can simply use JAXP without worrying about the particular parser implementation being used "under the hood." This pluggability layer provides a very nice abstraction from the underlying parser implementation, allowing developers to use an appropriate parser for their application (e.g., a validating or non-validating) without changing any code.
A problem occurs if the developer goes outside the realm of JAXP and uses some parser-specific class (i.e., something "under the hood") directly. This is the case with class XmlDocument. Class XmlDocument is not part of JAXP, but is the concrete implementation of interface org.w3c.dom.Document that Sun's default parser uses to represent an XML document. Because it is a Sun-internal class and not part of JAXP, it isn't guaranteed to be stable between versions of JAXP, as you have discovered. We used class XmlDocument in the book to write out an XML document over a stream because, at the time of writing, there was no standard method in JAXP for doing this. The only way to write an XML document from memory to a file or over a network was to use one of the parser-internal classes.
JAXP 1.1 and higher provide a new way to write XML documents to streams that is part of the standard API. To use the new, standard method, add the following import statements in Fig. 8.10 (you can remove in the import of org.apache.crimson.tree.XmlDocument):
and replace the lines
( (XmlDocument) document).write( new FileOutputStream
( "intro1.xml" ) );
with the lines
TransformerFactory transformerFactory =
Transformer serializer = transformerFactory.newTransformer();
serializer.transform( new DOMSource( document ),
new StreamResult( new FileOutputStream
( "intro1.xml" ) ) );
The first two lines use a TransformerFactory to create a Transformer. A Transformer is simply an XSLT processor (see Chapter 12 of XML How to Program for a discussion of XSLT). The last three lines invoke method transform of class Transformer to take the document, which is represented by a DOM tree in memory, and write it out to the given FileOutputStream. The Transformer simply transforms the XML document from its representation in memory to a new representation as a string of characters written to a stream.