Java 如何修复错误“org.xml.sax.SAXParseException / Content is not allowed in prolog”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33141782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix the error "org.xml.sax.SAXParseException / Content is not allowed in prolog"?
提问by Dmitrii Pisarenko
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.
我有以下课程有两种方法。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);
}
}
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
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)
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.
您可以在此处找到我正在尝试阅读的源代码和 XML 文件。
How can I fix this error?
我该如何解决这个错误?
Update 1:Changing openStream
to
更新 1:更改openStream
为
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:
更新 2:
If I change the openInputStream
method like that
如果我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;
}
I get this output:
我得到这个输出:
BOM: false
[Fatal Error] :1:1: Content is not allowed in prolog.
BOM: false
[Fatal Error] :1:1: Content is not allowed in prolog.
采纳答案by Pieter Kuijpers
In XmlReader.openInputStream
, change
在XmlReader.openInputStream
,改变
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.
IOUtils.toInputStream
将给定的 String 转换为 InputStream,在这种情况下是包含路径的 String,而不是文件本身。
回答by Amedeo
To solve this problem I had to properly set the Deployment Descriptor in web.xml for my Tomcat application.
为了解决这个问题,我必须在 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>
回答by user2561288
If some empty space at starting of the xml file shows this issue.
如果 xml 文件开头的一些空白区域显示此问题。
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.
但是,如果您使用 cURL 将文件从本地计算机传输到某个服务器,则在从本地到服务器的 POST 期间,您必须在路径开头提及符号“@”。
For example: If you want to post the file in some path, you have to specify the path as : "@D:/cURL/POST" etc..
例如:如果要在某个路径中发布文件,则必须将路径指定为:“@D:/cURL/POST”等。