Java: Using XML resolvers and DOM

Published: Sunday, 15 August 2004

Read the related article on XML resolvers to understand why we use resolvers.

Tutorial and Sample code

Download and install the xml-commons-resolver package which is part of the xml-commons components from apache. The example below assumes that you have already created your catalog file at D:\cygwin\usr\share\catalog\docbook.catalog, and we want to specify this explicitly within our code. There are other ways to setup the catalog, read the documentation to see which one suits your needs.

CatalogManager cm = new CatalogManager();
cm.setCatalogFiles("D:\\cygwin\\usr\\share\\catalog\\docbook.catalog");
cm.setIgnoreMissingProperties(true);
CatalogResolver cr = new CatalogResolver(cm);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
db = dbf.newDocumentBuilder();
db.setEntityResolver(cr);

Ensure you have the line

cm.setIgnoreMissingProperties(true);

otherwise it will output to standard error with:

Cannot find CatalogManager.properties

It is set to complain by default, but in this case it is turned off because we know we’ll be specifying the catalog files with the setCatalogFiles method.

Alternatively, you can use the system property xml.catalog.files to set the catalog files.