当我尝试按模式使用 DocumentBuilderFactory 验证 xml 文件时,我收到错误 "Cannot find the declaration of element "

When i try to validate xml file using DocumentBuilderFactory by schema i recieve error "Cannot find the declaration of element "

我有 xml 个名为 input.xml

的文件

包含以下代码

<?xml version="1.0"?>
<tests xmlns="http://www.example.com/Report"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.example.com/Report input.xsd">
    <test id="1">
        <coverage>65</coverage>
        <usedFramework>Junit4</usedFramework>
        <typeTest>Integration</typeTest>
    </test>
    <test id="2">
        <coverage>35</coverage>
        <usedFramework>Junit5</usedFramework>
        <typeTest>Module</typeTest>
    </test>
    <test id="3">
        <coverage>45</coverage>
        <usedFramework>Mockito</usedFramework>
        <typeTest>Integration</typeTest>
    </test>
</tests>

我还有 XML 名称为 input.std 的架构 XML 架构文件如下所示

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.example.com/Report"
           xmlns="http://www.example.com/Report"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name="tests">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="test" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="coverage" type="xs:positiveInteger"/>
                            <xs:element name="usedFramework" type="xs:string"/>
                            <xs:element name="typeTest" type="xs:string"/>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:positiveInteger"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

接下来的代码会写在java

try {
          File schemaFile = new File("input.xsd");
            SchemaFactory schemaFactory = SchemaFactory
                    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = schemaFactory.newSchema(schemaFile);

            File inputXml = new File(xmlPath);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
            factory.setSchema(schema);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputXml);
            List<Test> list = new ArrayList<>();
            NodeList tests = document.getElementsByTagName("test");
            for(int i=0;i< tests.getLength();i++){
                Element element = (Element) tests.item(i);
                if(element.getNodeType()==Node.ELEMENT_NODE){
                    int coverage = Integer.parseInt(element.getElementsByTagName("coverage").item(0).getTextContent());
                    int id = Integer.parseInt(element.getAttribute("id"));
                    String usedFramework = (element.getElementsByTagName("usedFramework").item(0).getTextContent());
                    String typeTest = (element).getElementsByTagName("typeTest").item(0).getTextContent();
                    list.add(new Test(id,coverage,usedFramework,typeTest));
                }
            }
            list.sort(Comparator.comparing(Test::getCoverage));
            list.forEach(System.out::println);

        }catch (ParserConfigurationException | IOException | SAXException e){
            
            Logger.getLogger(XmlReader.class.getName()).log(Level.INFO,e.getMessage());

        }

当我尝试 运行 应用程序时发生错误

那么我哪里出错了?我查看了“如何正确引用本地 XML 模式文件?”在不同的问题上,没有看到任何错误,你能帮我改正吗?

我必须调用 DocumentBuilderFactory 的方法 setNamespaceAware 并将 true 作为参数传递

// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("example.xml"));

问题是默认情况下 DocumentBuilder 不支持命名空间。以下附加更改解决了该问题:

// parse an XML document into a DOM tree
DocumentBuilderFactory dmfactory = DocumentBuilderFactory.newInstance();
dmfactory.setNamespaceAware(true);
DocumentBuilder parser = dmfactory.newDocumentBuilder();
Document document = parser.parse(new File("example.xml"));