【问题标题】:Error: White spaces are required between publicId and systemId错误:publicId 和 systemId 之间需要空格
【发布时间】:2021-07-20 01:19:43
【问题描述】:

我认为我在 DOCTYPE 行的 XML 文件中犯了错误。我该如何解决我的问题?

我使用 DOM 库来解析 XML 文件。我的 XML 文件名为 firstTime.xml:

<?xml version="1.0"?>
    <!DOCTYPE tests PUBLIC "firstTime.xsd">
    <tests xmlns="http://www.example.com/Report"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.example.com/Report firstTime.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>

我的 XSD 文件名为 firstTime.xsd:

<?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 inputXml = new File(xmlPath);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
            factory.setValidating(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputXml);
        

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

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

在我通过添加此行更改 xml 文件之后

<!DOCTYPE tests SYSTEM "firstTime.xsd">
<!DOCTYPE tests PUBLIC "1" "firstTime.xsd">

我收到此错误

【问题讨论】:

    标签: java xml xsd xml-parsing domparser


    【解决方案1】:

    使用PUBLIC,您需要两个字符串:

    ExternalID ::= 'SYSTEM' S SystemLiteral
                 | 'PUBLIC' S PubidLiteral S SystemLiteral
    

    the XML Spec

    【讨论】:

      【解决方案2】:

      替代解决方案:

      1. 强烈推荐:删除DOCTYPE这一行;它很少与 XSD 一起使用。

      2. DOCTYPE 行修复为SYSTEM,并引用一个DTD:

        <!DOCTYPE tests SYSTEM "firstTime.dtd">
        
      3. 修复DOCTYPE 行以包含publicId 和systemId,并引用DTD:

        <!DOCTYPE tests PUBLIC "your-public-id-here" "firstTime.dtd">
        

      另见

      【讨论】:

      • 请注意,您的新错误表明它需要 DTD 的系统标识符,但您正在向 XSD 提供一个系统标识符。我更新了答案以强调您几乎肯定不想将 DOCTYPE 行与 XSD 一起使用。
      猜你喜欢
      • 2015-04-14
      • 1970-01-01
      • 2016-02-19
      • 2013-04-18
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多