如何修复错误“org.xml.sax.SAXParseException /内容不允许在prolog中”?

时间:2015-10-15 06:53:11

标签: java xml xml-parsing

我有以下两个方法的课程。 openInputStream创建一个输入流,随后将其输入readDocument以从该流创建XML文档。

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);
    }
}

然后我进行了以下测试:

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抛出异常

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)

您可以找到我正在尝试阅读的源代码和XML文件here

如何解决此错误?

更新1:openStream更改为

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

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

没有帮助。

更新2:

如果我改变那样的openInputStream方法

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;
}

我得到了这个输出:

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

3 个答案:

答案 0 :(得分:1)

XmlReader.openInputStream中,更改

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

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

IOUtils.toInputStream将给定的String转换为InputStream,在本例中为包含路径的String,而不是文件本身。

答案 1 :(得分:1)

如果xml文件启动时出现空格,则会显示此问题。

但是如果您使用cURL将文件从本地计算机传输到某个服务器,则在从本地到服务器的POST期间,在路径之前必须在路径的开头提及符号“@”。

例如:如果要在某个路径中发布文件,则必须将路径指定为:“@ D:/ cURL / POST”等。

答案 2 :(得分:-1)

要解决此问题,我必须在web.xml中为我的Tomcat应用程序正确设置部署描述符。

<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>