This page documents the various Document Object Model (DOM) samples included with Xerces. Besides being useful programs, they can be used as DOM programming examples to learn how to program using the DOM API.

Basic DOM samples:

Most of the DOM parser samples have a command line option that allows the user to specify a different DOM parser to use. In order to supply another DOM parser besides the default Xerces DOMParser, a DOM parser wrapper class must be written. This class must implement the dom.ParserWrapper interface.

JAXP could be used instead of the special DOM parser wrapper class. However, that feature is not implemented at this time. Using JAXP would require the user to specify the -Djavax.xml.parsers.DocumentBuilderFactory=... option to the virtual machine in order to use a different document builder factory.

A sample DOM counter. This sample program illustrates how to traverse a DOM tree in order to get information about the document. The output of this program shows the time and count of elements, attributes, ignorable whitespaces, and characters appearing in the document. Three times are shown: the parse time, the first traversal of the document, and the second traversal of the tree.

This class is useful as a "poor-man's" performance tester to compare the speed and accuracy of various DOM parsers. However, it is important to note that the first parse time of a parser will include both VM class load time and parser initialization that would not be present in subsequent parses with the same file.

The results produced by this program should never be accepted as true performance measurements. java dom.Counter (options) uri ...
OptionDescription
-p nameSelect parser wrapper by name.
-x numberSelect number of repetitions.
-n | -NTurn on/off namespace processing.
-v | -VTurn on/off validation.
-s | -S Turn on/off Schema validation support.
NOTE: Not supported by all parsers.
-f | -F Turn on/off Schema full checking.
NOTE: Requires use of -s and not supported by all parsers.
-hs | -HS Turn on/off honouring of all schema locations.
NOTE: Requires use of -s and not supported by all parsers.
-va | -VA Turn on/off validation of schema annotations.
NOTE: Requires use of -s and not supported by all parsers.
-dv | -DV Turn on/off dynamic validation.
NOTE: Not supported by all parsers.
-xi | -XI Turn on/off XInclude processing.
NOTE: Not supported by all parsers.
-xb | -XB Turn on/off base URI fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-xl | -XL Turn on/off language fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-hDisplay help screen.

A sample DOM filter. This sample program illustrates how to use the Document#getElementsByTagName() method to quickly and easily locate elements by name.

java dom.GetElementsByTagName (options) uri ...
OptionDescription
-p nameSelect parser wrapper by name.
-e nameSpecify element name for search.
-a nameSpecify attribute name for specified elements.
-n | -NTurn on/off namespace processing.
-v | -VTurn on/off validation.
-s | -S Turn on/off Schema validation support.
NOTE: Not supported by all parsers.
-f | -F Turn on/off Schema full checking.
NOTE: Requires use of -s and not supported by all parsers.
-hs | -HS Turn on/off honouring of all schema locations.
NOTE: Requires use of -s and not supported by all parsers.
-va | -VA Turn on/off validation of schema annotations.
NOTE: Requires use of -s and not supported by all parsers.
-dv | -DV Turn on/off dynamic validation.
NOTE: Not supported by all parsers.
-xi | -XI Turn on/off XInclude processing.
NOTE: Not supported by all parsers.
-xb | -XB Turn on/off base URI fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-xl | -XL Turn on/off language fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-hDisplay help screen.

A sample DOM writer. This sample program illustrates how to traverse a DOM tree in order to print a document that is parsed.

java dom.Writer (options) uri ...
-p nameSelect parser wrapper by name.
-n | -NTurn on/off namespace processing.
-v | -VTurn on/off validation.
-xd | -XD Turn on/off loading of external DTDs.
NOTE: Always on when -v in use and not supported by all parsers.
-s | -S Turn on/off Schema validation support.
NOTE: Not supported by all parsers.
-f | -F Turn on/off Schema full checking.
NOTE: Requires use of -s and not supported by all parsers.
-hs | -HS Turn on/off honouring of all schema locations.
NOTE: Requires use of -s and not supported by all parsers.
-va | -VA Turn on/off validation of schema annotations.
NOTE: Requires use of -s and not supported by all parsers.
-ga | -GA Turn on/off generation of synthetic schema annotations.
NOTE: Requires use of -s and not supported by all parsers.
-dv | -DV Turn on/off dynamic validation.
NOTE: Not supported by all parsers.
-xi | -XI Turn on/off XInclude processing.
NOTE: Not supported by all parsers.
-xb | -XB Turn on/off base URI fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-xl | -XL Turn on/off language fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-c | -C Turn on/off Canonical XML output.
NOTE: This is not W3C canonical output.
-hDisplay help screen.

This sample program illustrates how to use the DOM Level 3 API.

java dom.DOM3 uri

A sample of Adding lines to the DOM Node. This sample program illustrates:

  • How to override methods from DocumentHandler ( XMLDocumentHandler)
  • How to turn off ignorable white spaces by overriding ignorableWhiteSpace
  • How to use the SAX Locator to return row position (line number of DOM element)
  • How to attach user defined Objects to Nodes using the DOM Level 3 setUserData method.
java dom.DOMAddLines (options) uri ...
OptionDescription
-hDisplay help screen.
-iDon't print ignorable white spaces.