SAX validation

So far we only parsed well formed document instances. Our current parser may operate on valid XML instances:

Figure 971. An invalid XML document. Create comment in forum
<xs:element name="catalog">
      <xs:element ref="item"/>

<xs:element name="item">
  <xs:complexType mixed="true">
    <xs:attribute name="orderNo" type="xs:int" use="required"/>
  <item orderNo="3218">Swinging headset</item>
  <item orderNo="9921">200W Stereo Amplifier</item> <!-- second entry forbidden
                                                                      by schema -->

In contrast to Figure 970, “A non well formed document. ” this document is well formed. But it is not valid with respect to the schema since more than one <item> elements are present.

This document instance is well-formed but not valid: Only one element <item> is allowed due to an ill-defined schema. The parser will not report any error or warning. In order to enable validation we need to configure our parser:

xmlReader.setFeature("", true);

The string serves as a key. Since this is an ordinary string value a parser may or may not implement it. The SAX standard defines two exception classes for dealing with feature related errors:


The feature is not known to the parser.


The feature is known to the parser but the parser does not support it or it does not support a specific value being set as a value.

The xml-commons resolver project offers an implementation being able to process various catalog file formats. Maven based project allow the corresponding library import by adding the following dependency:


We need a properties file defining XML catalogs to be used and additional parameters:

# Catalogs are relative to this properties file
# Catalog list

# PUBLIC in favour of SYSTEM

This configuration uses some catalogs from the OxygenEclipse™ plugin. We may now add a resolver to our SAX application by referencing the above configuration file ❶ and registering the resolver to our SAX parser instance ❷:

xmlReader = saxParser.getXMLReader();

      // Set up resolving PUBLIC identifier
      final CatalogManager cm = new CatalogManager("" ❶ );
      final CatalogResolver resolver = new CatalogResolver(cm);
      xmlReader.setEntityResolver(resolver) ❷;