## Parsing existing XML documents

JDom uses SAX for parsing XML documents. Rather than handling SAX events ourselves we prefer a DOM (in memory) representation of our document. The simple catalog serves as an introductory example:

We already noticed the need for an ErrorHandler instance during SAX processing. A DOM Parser requires a means to communicate parsing errors in case of XML related errors. A DOM parser implementor is free to choose his implementation but most implementations are based on top of a so called SAX parser. The SAX parser in turn defines a standard ErrorHandler interface we may use for the sake of conveying parsing errors:

 Error and warning messages go here. The underlying stream may be linked e.g. to a file or to standard output. Internal method assembling the actual error or warning message containing a line number / column number based file reference. Constructor for defining the respective stream. Callback method being called in case of minor problems i.e. a missing mandatory attribute. The exception instance holding a detailed problem description Severe error. Fatal error e.g. indicating improper nesting of elements prohibiting further parsing of the given XML input.

We use this bit to assemble a DOM based XML parsing application:.

 The parser workhorse. Though an ErrorHandler is not strictly being required it allows for localization of XML document parsing errors and warnings. Descending a catalog till its  elements. For each  its name and order number are being written to the output. Parsing error being thrown in i.e. case of non- wellformed catalog documents. Parsing XML input file relative to the project's src/main/resources folder. Accessing the document's root element Streaming all  children of .

Execution of process(...) requires a driver instance providing an XML input filename: