szolo
szolo

Reputation: 247

Validating XML against XSD with XMLReader

There are a lot of manuals in the web about this validating procedure. In spite of this fact I can't find the reason why my code doesn't work in proper way. Values of input schema and xml I got here.

static String schemaString ="<?xml version=\"1.0\"?>" +
        "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" +
        " targetNamespace=\"http://www.java2s.com\"" +
        " xmlns=\"http://www.java2s.com\"" +
        " elementFormDefault=\"qualified\">" +
        "<xs:element name=\"note\">" +
        "<xs:complexType>" +
        "<xs:sequence>" +
        "<xs:element name=\"to\" type=\"xs:string\"/>" +
        "<xs:element name=\"from\" type=\"xs:string\"/>" +
        "<xs:element name=\"heading\" type=\"xs:string\"/>" +
        "<xs:element name=\"body\" type=\"xs:string\"/>" +
        "</xs:sequence>" +
        "</xs:complexType>" +
        "</xs:element>" +
        "</xs:schema>";

static String xmlString = "<?xml version=\"1.0\"?>" +
        "<note>" +
        "<to>rtoName</to>" +
        "<from>FromName</from>" +
        "<heading>Info</heading>" +
        "<body>Message Body</body>" +
        "</note>";

    String xml;
    XMLReader xmlReader = null;
    try {

        SAXSource source = new SAXSource(new InputSource(new StringReader(schemaString)));
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(source);

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setSchema(schema);
        SAXParser saxParser = spf.newSAXParser();
        xmlReader = saxParser.getXMLReader();
    } catch (Exception e) {
        e.printStackTrace();
    } 

    // parsing step after all preconditions succeeded
    try {
          xmlReader.parse(new InputSource(new StringReader(xmlString)));
    } catch (Exception e) {
        e.printStackTrace();   
    }

Execution result is something like this:

[Error] :1:28: cvc-elt.1: Cannot find the declaration of element 'note'.

In debugger mode everything seems to be fine. Schema is setted correctly and so on. Ideas?

Upvotes: 2

Views: 3213

Answers (2)

Less
Less

Reputation: 3207

Is there a particular reason why you are using XMLReader?
I'd just like to present another way, you might find it useful:

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;      

// ...

   try {
        // load schema from file
        File schemaFile = new File(schemaLocation);
        // load xml source form string holding the content
        Source xmlFile = new StreamSource(new StringReader(fileContent));

        SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        Schema schema = schemaFactory.newSchema(schemaFile);

        Validator validator = schema.newValidator();

        validator.validate(xmlFile);

        System.out.println("XML is valid");

    } catch (Exception e) {

        System.out.println("XML is NOT valid");
        System.out.println("Reason: " + e.getMessage());

    }

Upvotes: 2

DRCB
DRCB

Reputation: 2121

I guess the problem is undefined namespace in your XML.

Try to set a namespace explicitly (see your targetNamespace in XSD:

static String xmlString = "<?xml version=\"1.0\"?>" +
    "<note xmlns=\"http://www.java2s.com\">" +
    "<to>rtoName</to>" +
    "<from>FromName</from>" +
    "<heading>Info</heading>" +
    "<body>Message Body</body>" +
    "</note>";

Upvotes: 2

Related Questions