2

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")));
9
  • i think you miss to set the Schema, validating is true but where is the Schema? docs.oracle.com/javase/7/docs/api/javax/xml/parsers/… Commented Jun 22, 2015 at 14:40
  • I think it should use the schema specified by xmlns="http://schemas.mandiant.com/2010/ioc" Commented Jun 22, 2015 at 14:41
  • nope, is the xml namespace that use to match with schema but there is no autamatic download of the schema, consider off-line schenarious. If you want to validate your xml you need to load the schema and set into the factory Commented Jun 22, 2015 at 14:44
  • 2
    You would need to add an attribute 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 a file:/dir/subdir/schema.xsd of course instead of an HTTP URL. Commented Jun 22, 2015 at 14:45
  • 1
    The xsi:schemaLocation attribute value should be a list of pairs of URLs where for each pair the first value is the URL of the namespace (in your case http://schemas.mandiant.com/2010/ioc) and the second URL provides the location of the schema for that namespace so xsi: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. Commented Jun 22, 2015 at 14:55

2 Answers 2

2

I can't say what is wrong with the implementation you provided but here is a method I use to validate XML document against a schema (using only standard libraries)

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


    /**
     * 
     * Purpose: validate an XML document against a schema
     * @param dom The org.w3c.dom.Document object representing the XML document
     * @param xsdPath The path to the XML schema file
     * @return True if the XML instance validates against the schema.
     */
    private static boolean validateXML(Document dom, String xsdPath){
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaFile = new StreamSource(new File(xsdPath));
        try {
            Schema schema = factory.newSchema(schemaFile);
            Validator validator = schema.newValidator();
            // validate the DOM tree
            validator.validate(new DOMSource(dom));
        } catch (SAXException se) {
            se.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The solution is to add schemaLocation attribute with all required dependencies.

xsi:schemaLocation="http://schemas.mandiant.com/2010/ioc 
http://schemas.mandiant.com/2010/ioc/ioc.xsd 
http://schemas.mandiant.com/2010/ioc/TR/ 
http://schemas.mandiant.com/2010/ioc/TR/ioc-TR.xsd"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.