在Java中解析XML时没有工作ID属性

时间:2019-06-26 10:29:10

标签: java xml validation dom

我目前正在使用图形XML编辑器,必须通过其ID属性访问各个元素,这一点至关重要。 我建立了一个XML模式,在其中定义了ID属性。 我使用javax.xml.parsers.DocumentBuilderFactory并通过相应的SchemaFactory将架构分配给它。到目前为止,我的XML文档的验证也有效,如果不遵守约束条件,我会收到错误消息。但是Attr.isId()始终返回false。

提示可能是以下错误消息: 错误:URI =空行= 2:文档根元素“ TABS”必须与DOCTYPE根“ null”匹配。 在我看来,这就像解析器正在尝试使用DTD而非模式来验证XML文档。 我正在使用Java 1.8。

解析文档并加载架构

    public static Document generateDom(IDocument xmlDocument) {
        String editorText = xmlDocument.get();
        StringReader reader = new StringReader(editorText);
        InputSource inputSource = new InputSource(reader);

        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        setSchema(documentFactory);
        documentFactory.setValidating(true);
        Document document = null;

        try {
            DocumentBuilder builder = documentFactory.newDocumentBuilder();
            document = builder.parse(inputSource);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            // TODO ErrorHandling
            e.printStackTrace();
        }
        return document;
    }

    private static void setSchema(DocumentBuilderFactory documentFactory) {
        URL url = DomLoader.class.getResource("..\\..\\..\\resources\\scriptGenerator.xsd");
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = null;
        try {
            schema = schemaFactory.newSchema(url);
        } catch (SAXException e1) {
            // TODO ErrorHandling
            e1.printStackTrace();
        }
        documentFactory.setSchema(schema);
    }

模式定义摘录

    <xs:element name="GROUP">
        <xs:complexType>
            <xs:sequence>
                <xs:sequence>
                    <xs:element ref="SQL" />
                </xs:sequence>
                <xs:sequence minOccurs="0" maxOccurs="unbounded">
                    <xs:choice>
                        <xs:element ref="TEXT" />
                        <xs:element ref="BUTTON" />
                        <xs:element ref="ALWAYS" />
                    </xs:choice>
                </xs:sequence>
            </xs:sequence>
            <xs:attribute name="id" use="required" type="xs:ID" />
            <xs:attribute name="name" use="required" type="xs:string" />
        </xs:complexType>
    </xs:element>

我的.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<TABS>
    <TAB id="ID_ea1270bc-2554-4291-b5ad-8c7802b5fc49" name="Tab 1">
        <GROUP id="ID_3155171c-d2ac-4717-aa38-009005c79e18" name="Group 1">
            <SQL>
                <PREPARE>
                    <CONSTANT/>
                </PREPARE>
                <MERGE>
                    <CONSTANT/>
                </MERGE>
                <SYNCHRONIZE>
                    <CONSTANT/>
                </SYNCHRONIZE>
            </SQL>
        </GROUP>
    </TAB>
</TABS>

如果使用Attr.isId(),我的控制台输出:

id | false
name | false
id | false
name | false
id | false
name | false
type | false

2 个答案:

答案 0 :(得分:0)

好吧,我至少找到了解决该错误的方法

Error: URI=null Line=2: Document root element "TABS" must match DOCTYPE root "null"

如果使用DocumentFactory.setValidating(true) DocumentBuilder实际上需要DTD而不是模式。因此,关闭此功能也会使错误消失。

答案 1 :(得分:0)

然后我解决了这个问题:错误在于我的.xsd文件的命名空间。 解决方法如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    targetNamespace="http://www.w3.org/2001/scriptGenerator"
    xmlns="http://www.w3.org/2001/scriptGenerator">
    <xs:element name="TABS">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="unbounded" ref="TAB" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

我还必须修复.xml文件的命名空间

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