Search code examples
javaxmlxsdxml-parsingdomparser

Error: White spaces are required between publicId and systemId


I think I made mistakes in XML file in DOCTYPE line. How can I solve my problem?

I using DOM library for parsing XML file. My XML file is named 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>

My XSD file is named 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>

My code using 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());
        }

Error occurs when I'm trying to run java app:

enter image description here

after i changed xml file by adding this lines

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

i recieve this error

enter image description here


Solution

  • Alternative solutions:

    1. Strongly recommended: Delete the DOCTYPE line; it's rarely used with XSD.

    2. Repair the DOCTYPE line to be SYSTEM, and reference a DTD:

      <!DOCTYPE tests SYSTEM "firstTime.dtd">
      
    3. Repair the DOCTYPE line to include both a publicId and a systemId, and reference a DTD:

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

    See also