XML without a namespace. Confirm compliance with one of several XSD

I have a situation where we regularly get a bunch of XML files. We do not control them, and they have no namespace information, and we really would like to avoid changing them.

We have an XSD that we need to use to validate XML files, and which works if explicitly encoded for use. Now we would like to hint at the SAX parser that this particular XML dialect should be checked against this XSD (which we have in the file system), but I cannot find any other way than providing noNamespaceSchemaLocation in the XML file, which we really like to avoid.

Suggestions? Will an EntityResolver always be called with an empty / empty namespace?

(functional solution will give 500 bonus points when I will be allowed)

+4
source share
1 answer

Using java.xml.validation, you can specify the XSD schema that should be used to validate the XML document without referencing the document:

import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

...
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File("<path to the xsd>"));

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(false);
spf.setSchema(schema);

XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setContentHandler(...);
xmlReader.parse(new InputSource(...)); // will validate against the schema

Note that this setValidating()only means disabling DTD validation as determined by the W3C . This call will not be strictly necessary since the default value is false.

+3
source

Source: https://habr.com/ru/post/1607672/


All Articles