报错信息
org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j.DocumentHelper.parseText(DocumentHelper.java:278)
中文错误信息:
org.dom4j.DocumentException: Error on line 1 of document :前言中不允许有内容.
报错代码
try {
Document document = DocumentHelper.parseText(xmlContext);
} catch (Exception e) {
e.printStackTrace();
}
异常分析
该错误通常由XML文件格式问题引起,UTF-8编码的XML文件若包含BOM头(EF BB BF字节序标记),会导致解析器识别异常。
解决办法
Java代码中通过BOMInputStream过滤XML文件中的BOM头。参考代码如下:
try {
InputStream in = new ByteArrayInputStream(xmlStr1.getBytes());
BOMInputStream bomIn = new BOMInputStream(in, false);
cleanXml = IOUtils.toString(bomIn, StandardCharsets.UTF_8);
xmlStr1 = cleanXml;
}finally {
//关闭流
}
1621

被折叠的 条评论
为什么被折叠?



