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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With