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:

Figure 783. XML catalog sample data Slide presentation Create comment in forum
<catalog>
  <item orderNo="3218">Swinging headset</item>
  <item orderNo="9921">200W Stereo Amplifier</item>
</catalog>

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:

Figure 784. SAX error handler Slide presentation Create comment in forum
public class MySaxErrorHandler implements ErrorHandler {

   private PrintStream out; //The error handler's output goes here

   private String getParseExceptionInfo (SAXParseException ex) {
     return "Error '" + ex.getMessage() + "' at line " + ex.getLineNumber() +
                  ", column " + ex.getColumnNumber();
   }
   public MySaxErrorHandler(final PrintStream out ) {this.out = out;}
   @Override public void warning (SAXParseException exception ) throws SAXException {
      out.print("Warning:" + getParseExceptionInfo(exception));
   }
   @Override public void error (SAXParseException exception ) throws SAXException {
      out.print("Error:" + getParseExceptionInfo(exception));
   } @Override
   public void fatalError (SAXParseException exception ) throws SAXException {
      out.print("Fatal error:" + getParseExceptionInfo(exception));
   }
}

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:.

Figure 785. Accessing an XML Tree purely by DOM methods. Slide presentation Create comment in forum
public class ReadCatalog {
   private SAXBuilder builder = new SAXBuilder(); 

   public ReadCatalog() {
      builder.setErrorHandler(new MySaxErrorHandler(System.out)); 
   }
   public void process(final String filename)  throws JDOMException , IOException {
      final Document docInput = builder.build(
            getClass().getClassLoader().getResource(filename) 
      );
      final Element docRoot = docInput.getRootElement(); 
      docRoot.getChildren().forEach(item -> 
      System.out.println(
            "Article: " + item.getText() +
            ", order number: " + item.getAttributeValue("orderNo")));
   }
}

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 <item> elements. For each <item> 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 <catalog>

Streaming all <item> children of <catalog>.

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

Figure 786. Driver class execution entry point Slide presentation Create comment in forum
public class ReadCatalogDriver {

  public static void main(String[] argv) throws Exception {
    final ReadCatalog catalogReader = new ReadCatalog();
    catalogReader.process("catalog.xml");
  }
}
Article: Swinging headset, order number: 3218
Article: 200W Stereo Amplifier, order number: 9921