简体   繁体   English

XML验证错误,找不到元素(我认为即使元素存在并且架构和xml都已正确加载)

[英]XML validation error, Element not found (even though element exists and schema and xml are loaded properly, I think)

I'm trying to validate a simple XML file against an XML schema using the following chunk of code. 我正在尝试使用以下代码块针对XML模式验证一个简单的XML文件。 I'm getting an exception 我要例外了

org.xml.sax.SAXParseException: cvc-elt.1: 
    Cannot find the declaration of element 'Applications'

and was wondering if anyone had any idea why that could be. 并想知道是否有人知道为什么会这样。 (When I try looking at what the contents of the schema are after it is loaded, I see a bunch of grammar objects or something of that sort, with nothing indicating that the schema is loaded properly. Could that be it? If I try loading a schema with an non-existing file name, it gives me a file not found exception...so I'm getting it is finding the right schema when the right file name is given) (当我尝试查看架构加载后的内容时,我看到了一堆语法对象或类似的东西,没有任何东西表明架构已正确加载。可以吗?如果我尝试加载一个不存在的文件名的模式,它给了我一个文件未找到的异常...所以我得到的是,当给出正确的文件名时它正在寻找正确的模式)

public void getPriceSummaryInfo(){
        Document doc = null;
        try {
            File fXmlFile = new File("testXML.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(fXmlFile);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
        Schema schema = getSchema("testSchema.xsd");
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(doc));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Schema getSchema (String xsdPath) throws Exception {
        assert xsdPath != null : "XML schema path is null.";
        SchemaFactory fact = 
                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        Schema schema;
        try {
            schema = fact.newSchema(new StreamSource(new File(xsdPath)));
        }
        catch (SAXException e) {
            throw new Exception(
                    "Unable to find target schema to validate XML.", e);
        }
        return schema;
    } 

The xml and xsd files are: xml和xsd文件是:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.randomthing.com"
            xmlns="http://www.randomthing.com"
            elementFormDefault="qualified">

    <xsd:element name="Applications">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Application" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="Application">
        <xsd:annotation>
            <xsd:documentation>
             blah
            </xsd:documentation>
        </xsd:annotation>
       <xsd:complexType>
            <xsd:attribute name="id" type="xsd:long" use="required"/>
            <xsd:attribute name="name" type="xsd:string" use="required"/>
            <xsd:attribute name="order" type="xsd:long" use="required"/>
        </xsd:complexType>    
    </xsd:element>
</xsd:schema>  



<?xml version="1.0" encoding="UTF-8"?>
<Applications xmlns ="http://www.randomthing.com"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.randomthing.com testSchema.xsd">

    <Application id="3" name="Blah" order="2">
    </Application>
</Applications>

Would appreciate any pointers! 将不胜感激任何指针! Thanks 谢谢

您应该使用命名空间来解析文档,因为它们在加载时会丢失。

dbFactory.setNamespaceAware(true);

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

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