Java “如何修复错误”;prolog“中不允许org.xml.sax.SAXParseException/Content;?
我有以下两种方法的课程Java “如何修复错误”;prolog“中不允许org.xml.sax.SAXParseException/Content;?,java,xml,xml-parsing,Java,Xml,Xml Parsing,我有以下两种方法的课程openInputStream创建一个输入流,然后将其送入readDocument以从该流创建XML文档 public class XmlReader { protected InputStream openInputStream(final String path) throws IOException { final InputStream inputStream; inputStream = IOUtil
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;行号:1;列数:1;prolog中不允许包含内容。
位于com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
位于com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
位于javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
位于XmlReader.readDocument(XmlReader.java:26)
位于XmlReaderTests.parsingOfMapFiles(XmlReaderTests.java:40)
您可以找到我试图阅读的源代码和XML文件
如何修复此错误
更新1:将openStream
更改为
protected InputStream openInputStream(final String path)
throws IOException {
return new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
}
或
没有帮助
更新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;
}
我得到这个输出:
物料清单:错误
[致命错误]:1:1:prolog中不允许包含内容。
在XmlReader.openInputStream
中,更改
inputStream = IOUtils.toInputStream(path, "UTF-8");
到
将给定字符串转换为InputStream,在本例中是包含路径的字符串,而不是文件本身。要解决此问题,我必须在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>
[...]
如果xml文件开头的一些空白显示此问题
但是,如果您使用cURL将文件从本地计算机传输到某个服务器,在从本地发送到服务器的过程中,在路径开始之前,您必须在路径的开头提到符号“@”
例如:如果要在某个路径中发布文件,则必须将路径指定为:“@D:/cURL/post”等 看这里:@Terrence,谢谢。请参阅我的更新1。可能是您的xml文件格式不正确(或为空)。在这里查看:www.xmlvalization。com@TommasoDiBucchianico不大可能发生的来自同一源且具有相同结构的另一个文件的解析没有问题。可能是空的?据我所知,“prolog中不允许内容”是由格式不好的文档引起的。非常感谢您的回答。现在可以了。但是,为什么这项工作和我最初的解决方案不起作用呢?
inputStream = new FileInputStream(new File(path));
<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>