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

时间:2022-10-02 23:40:36

I'm trying to validate a simple XML file against an XML schema using the following chunk of code. I'm getting an exception

我正在尝试使用以下代码块来针对XML模式验证简单的XML文件。我得到了一个例外

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

非常感谢任何指针!谢谢

1 个解决方案

#1


1  

You should parse the document with namespaces, you are losing them at load time.

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

dbFactory.setNamespaceAware(true);

#1


1  

You should parse the document with namespaces, you are losing them at load time.

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

dbFactory.setNamespaceAware(true);