I have a situation where we receive a bunch of XML files on a regular basis. We have no control over them, and they do not have namespace information, and we would really like to avoid changing them.
We have an XSD which we need to use to validate the XML files, and which works if explicitly coded to be applied. Now we would like to hint to a SAX parser that this particular XML dialect should be validated against this XSD (which we have on the file system), but I cannot find any other way than providing a noNamespaceSchemaLocation in the XML file which we really would like to avoid.
Suggestions? Will an EntityResolver always be called with a null/empty namespace?
(a functional solution will give 500 bonus points when I am allowed to)
Schemas can be composed of one or more XML documents. These schema documents can be explicitly joined together using the include and import elements.
At the sender's side, the validating software should be installed at the document generation point. Thus, each business message is validated and the possible errors can be corrected before sending.
XML Validator - XSD (XML Schema) XSD files are "XML Schemas" that describe the structure of a XML document. The validator checks for well formedness first, meaning that your XML file must be parsable using a DOM/SAX parser, and only then does it validate your XML against the XML Schema.
Using java.xml.validation
you can specify the XSD schema which should be used to validate a XML document without being referenced by 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 setValidating()
only means to turn off DTD validation as defined by the W3C. That call would not strictly be necessary since the default is false.
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