Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Xerces-j to validate an XML Document

I'm trying to validate an XML document using Xerces-J.

I want the validator to pick up and resolve any associated XSD or DTD files (using schemalocation, nonamespaceschemalocationa and DOCTYPE references). It seems the loading of these resources can be delegated to a Resolver class.

However all the samples I've seen start off creating a validator from a schema.

Is it possible to drive this the other way around, ask xerces to validate the XML document, and have it load what it needs, or must I first parse the XML file looking for schema references, load them, then create a validator from the schemas?

In an ideal world the validator would also support xsd 1.1

like image 322
Sprotty Avatar asked Oct 21 '22 11:10

Sprotty


1 Answers

You provide a parser with an EntityResolver to use when looking up <!DOCTYPE declarations or schema attributes. The most common entity resolver uses catalog files, which are essentially XML files or text files that define a dictionary of public IDs, system IDs, and URIs to physical files. See the org.apache.xml.resolver package. But you can also provide your own EntityResolver implementation.

CatalogResolver resolver = new CatalogResolver();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setFeature("http://apache.org/xml/features/validation/dynamic", true);
DocumentBuilder parser = dbf.newDocumentBuilder();
parser.setEntityResolver(resolver);
Document doc = parser.parse(someFile);
like image 190
Chris Nitchie Avatar answered Oct 24 '22 10:10

Chris Nitchie