Glory to Russia
Glory to Russia

Reputation: 18760

How to fix the error "org.xml.sax.SAXParseException / Content is not allowed in prolog"?

I have following class with two methods. openInputStream creates an input stream, which is subsequently fed into readDocument to create XML document from that stream.

public class XmlReader {
    protected InputStream openInputStream(final String path)
            throws IOException {
        final InputStream inputStream;
        inputStream = IOUtils.toInputStream(path, "UTF-8");
        return inputStream;
    }
    protected Document readDocument(final InputStream stream)
            throws ParserConfigurationException, SAXException, IOException {
        final DocumentBuilderFactory dbfac =
                DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        return docBuilder.parse(stream);
    }
}

Then I have the following test:

public class XmlReaderTests {
    @Test
    public void parsingOfMapFiles() throws IOException,
            SAXException, ParserConfigurationException {
        final String[] dirs = new String[] {
                "01_Phoenix1",
                "02_Phoenix2",
                "03_Guadalupe",
                "04_SanDiego_MiramarRoad",
                "05_Berlin_Alexanderplatz",
                "06_Portland_ForestAvenue",
                "07_Hartford_LafayetteHudsonStreet",
                "08_FortWorth",
                "09_Charleston_CalhounStreetMeetingStreet",
                "10_LosAngeles_PershingSquare",
                "11_Uelzen"
        };
        for (final String dir : dirs) {
            final XmlReader objectUnderTest = new XmlReader();
            final String path =
                    String.format("src/test/resources/mc/E-2015-10-13_1/%s/map.osm",
                            dir);
            final File file = new File(path);
            Assert.assertTrue(file.canRead());
            final InputStream inputStream =
                    objectUnderTest.openInputStream(file.getAbsolutePath());
            final Document doc = objectUnderTest.readDocument(inputStream);
            Assert.assertNotNull(doc);
        }
    }
}

readDocument throws the exception

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121) at XmlReader.readDocument(XmlReader.java:26) at XmlReaderTests.parsingOfMapFiles(XmlReaderTests.java:40)

You can find the source code and the XML files I'm trying to read here.

How can I fix this error?

Update 1: Changing openStream to

protected InputStream openInputStream(final String path)
        throws IOException {
    return new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
}

or

protected InputStream openInputStream(final String path)
        throws IOException {
    return new BOMInputStream(IOUtils.toInputStream(path));
}

didn't help.

Update 2:

If I change the openInputStream method like that

protected InputStream openInputStream(final String path)
        throws IOException {
    BOMInputStream inputStream = new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
    System.out.println("BOM: " + inputStream.hasBOM());
    return inputStream;
}

I get this output:

BOM: false [Fatal Error] :1:1: Content is not allowed in prolog.

Upvotes: 1

Views: 38271

Answers (3)

user2561288
user2561288

Reputation: 51

If some empty space at starting of the xml file shows this issue.

But in case if you are using cURL to transfer files from your local machine to some server, during POST from local to server, before the path you have to mention the symbol "@" at the starting of the path.

For example: If you want to post the file in some path, you have to specify the path as : "@D:/cURL/POST" etc..

Upvotes: 1

Amedeo
Amedeo

Reputation: 1

To solve this problem I had to properly set the Deployment Descriptor in web.xml for my Tomcat application.

<web-app id="WebApp" 
   xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
   [...]
</web-app>

Upvotes: -1

Pieter Kuijpers
Pieter Kuijpers

Reputation: 7307

In XmlReader.openInputStream, change

inputStream = IOUtils.toInputStream(path, "UTF-8");

to

inputStream = new FileInputStream(new File(path));

IOUtils.toInputStream converts the given String to an InputStream, in this case the String containing the path, instead of the file itself.

Upvotes: 1

Related Questions