I'm trying to validate an xml with DOM parser. For some reason the parser doesn't recognize the specified namespace. What could be the problem?
It throws error:
Error: URI=null Line=5: cvc-elt.1: Cannot find the declaration of element 'ioc'.
The code:
String outputString = "<?xml version=\"1.0\" encoding=\"us-ascii\"?>\n" +
"<ioc\n" +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n" +
"xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n" +
"xmlns=\"http://schemas.mandiant.com/2010/ioc\" >\n" +
"</ioc>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(new ByteArrayInputStream(outputString.getBytes("UTF-8")));
xmlns="http://schemas.mandiant.com/2010/ioc"xsi:schemaLocation="http://schemas.mandiant.com/2010/ioc http://example.com/schema.xsd"I think to indicate the schema location to the parser. It could be afile:/dir/subdir/schema.xsdof course instead of an HTTP URL.xsi:schemaLocationattribute value should be a list of pairs of URLs where for each pair the first value is the URL of the namespace (in your casehttp://schemas.mandiant.com/2010/ioc) and the second URL provides the location of the schema for that namespace soxsi:schemaLocation="http://schemas.mandiant.com/2010/ioc http://schemas.mandiant.com/2010/ioc/ioc.xsd"is a meaningful example. Only one URL does not make sense.