简体   繁体   English

使用XMLReader针对XSD验证XML

[英]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 . 输入模式和xml的值我在这里

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? 想法?

I guess the problem is undefined namespace in your XML. 我想问题是您的XML中的未定义名称空间。

Try to set a namespace explicitly (see your targetNamespace in XSD: 尝试显式设置名称空间(请参阅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>";

Is there a particular reason why you are using XMLReader ? 使用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());

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM