Java Reference
In-Depth Information
<country>Canada</country>
</person>
Then you need to enable the parser for validation; this is discussed in Parsing XML with
DOM . Any elements in the document not valid according to the DTD will result in an excep-
tion being thrown.
See Also
Document Type Definitions are simpler to write than XML Schemas. In some parts of the in-
dustry, people seem to be going on the assumption that XML Schemas will completely re-
place DTDs, and they probably will, eventually. But other developers continue to use DTDs.
There are also other options for constraining structure and data types, including RelaxNG (an
ISO standard).
Generating Your Own XML with DOM and the XML
Transformer
Problem
You want to generate your own XML files or modify existing documents.
Solution
Generate a DOM tree; pass the Document and an output stream to a Transformer 's trans-
form() method.
Discussion
JAXP supports the notion of XML Transformer objects (in javax.xml.transform ), which
have wide-ranging applicability for modifying XML content. Their simplest use, however, is
as a “null transformer”—one that doesn't actually transform the XML—used to transport it
from an in-memory tree to an output stream or writer.
Create a Document consisting of nodes (either directly as in this example, or by reading it).
Create the Transformer , setting any properties (in the example we set the “indent” property,
Search WWH ::




Custom Search