使用XMLReader通过XSD验证XML

时间:2012-08-16 15:04:35

标签: java xml xsd xml-parsing xsd-validation

网上有很多关于此验证程序的手册。尽管如此,我找不到我的代码无法正常工作的原因。输入模式和xml的值得到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();   
    }

执行结果是这样的:

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

在调试器模式下,一切似乎都很好。架构设置正确等等。想法?

2 个答案:

答案 0 :(得分:2)

我想问题是你的XML中未定义的命名空间。

尝试明确设置名称空间(请参阅XSD中的targetNamespace

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>";

答案 1 :(得分:2)

您使用XMLReader时有特殊原因吗? 我想提出另一种方式,你可能会发现它很有用:

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());

    }