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

Mentiflectax

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.

Pieter Kuijpers

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to fix this TemplateSyntaxError at error in django

分類Dev

How to fix backgroundColor assignment error?

分類Dev

How to fix <class 'AttributeError'> error?

分類Dev

how to fix a simple error on xtext?

分類Dev

How to fix this class template error?

分類Dev

How to fix 'modprobe vboxdrv' error in virtualBox?

分類Dev

How to fix "Headers already sent" error in PHP

分類Dev

how to fix ruby bundle install error?

分類Dev

How to fix/ignore syntax error in github

分類Dev

How to fix a JSON_ERROR_UTF8

分類Dev

How to fix syntax error in postgresql UPSERT?

分類Dev

How to fix the error with dev-server watch?

分類Dev

How to fix this gettallheaders.php error in laravel?

分類Dev

How to fix 'Error in getter for watcher "isDark"' in Vue

分類Dev

how to fix expected identifier before ';' token error

分類Dev

How to fix "pip installation error on pillow"

分類Dev

Projectiles Image Error Problem How To FIx?

分類Dev

How to fix "Index signature is missing in type" error?

分類Dev

How to fix NavigationDuplicated error in Internet Explorer?

分類Dev

How to fix the error undefined variable "$labels"in Prometheus?

分類Dev

How to fix the phpmyadmin error "json extension is missing"?

分類Dev

How to fix this gradle dependency resolution error?

分類Dev

How to fix "authz.dll is corrupt" error?

分類Dev

How to validate / fix an error in Certbot renewal cron

分類Dev

How to fix this error in EF7?

分類Dev

How to fix javavascript error from browser and improve error logging

分類Dev

How to fix error: Error parsing XML: unbound prefix

分類Dev

How to fix error “Expected version spec in …” using pip install on Windows?

分類Dev

How do I fix TypeError: unhashable type: 'list' Error

Related 関連記事

  1. 1

    How to fix this TemplateSyntaxError at error in django

  2. 2

    How to fix backgroundColor assignment error?

  3. 3

    How to fix <class 'AttributeError'> error?

  4. 4

    how to fix a simple error on xtext?

  5. 5

    How to fix this class template error?

  6. 6

    How to fix 'modprobe vboxdrv' error in virtualBox?

  7. 7

    How to fix "Headers already sent" error in PHP

  8. 8

    how to fix ruby bundle install error?

  9. 9

    How to fix/ignore syntax error in github

  10. 10

    How to fix a JSON_ERROR_UTF8

  11. 11

    How to fix syntax error in postgresql UPSERT?

  12. 12

    How to fix the error with dev-server watch?

  13. 13

    How to fix this gettallheaders.php error in laravel?

  14. 14

    How to fix 'Error in getter for watcher "isDark"' in Vue

  15. 15

    how to fix expected identifier before ';' token error

  16. 16

    How to fix "pip installation error on pillow"

  17. 17

    Projectiles Image Error Problem How To FIx?

  18. 18

    How to fix "Index signature is missing in type" error?

  19. 19

    How to fix NavigationDuplicated error in Internet Explorer?

  20. 20

    How to fix the error undefined variable "$labels"in Prometheus?

  21. 21

    How to fix the phpmyadmin error "json extension is missing"?

  22. 22

    How to fix this gradle dependency resolution error?

  23. 23

    How to fix "authz.dll is corrupt" error?

  24. 24

    How to validate / fix an error in Certbot renewal cron

  25. 25

    How to fix this error in EF7?

  26. 26

    How to fix javavascript error from browser and improve error logging

  27. 27

    How to fix error: Error parsing XML: unbound prefix

  28. 28

    How to fix error “Expected version spec in …” using pip install on Windows?

  29. 29

    How do I fix TypeError: unhashable type: 'list' Error

ホットタグ

アーカイブ