I need to validate XML file against XML Schema so that the schema info is taken from the XML.
I have XML document which defines its namespace. Like this:
<?xml version="1.0" encoding="UTF-8"?> <myelement xmlns="mynamespace"> </myelement>
The schema location is not in the document so I'd need to tell the validator where is the schema for given namespace. Right now I do it the following way:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware( true ); DocumentBuilder parser = dbf.newDocumentBuilder(); Document document = parser.parse(new File("mydocument.xml")); String namespace = document.getChildNodes().item(0).getNamespaceURI(); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(new File(namespace + ".xsd")); Schema schema = factory.newSchema(schemaFile); Validator validator = schema.newValidator(); validator.setErrorHandler(new MyErrorHandler()); validator.validate(new DOMSource(document));
This way it works but it has two problems:
1) It is kind of clumsy that I have to parse the document manually first and create the schema although theoretically validator has all the information it needs to do it automatically.
2) If I validate Document object, I don't get correct line numbers of errors. To get correct line numbers I would have to parse the same document twice (first to get namespace and second to validate).
Does anybody know any better solution?
Is there a way to tell the parser that namespace1 corresponds to schema1.xsd, namespace2 to schema2.xsd etc. before parsing? Or can I write some kind of callback which the parser can use to ask the schema (e.g. I can give LSResourceResolver to SchemaFactory)?