Java SAXParseException:序言中不允许内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20551572/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:44:29  来源:igfitidea点击:

SAXParseException: Content is not allowed in prolog

javatomcatsax

提问by Dónal

I need to add the following file to my Tomcat's '/conf' directory:

我需要将以下文件添加到我的 Tomcat 的“/conf”目录中:

<?xml version="1.0" encoding="UTF-8"?>
<Context useHttpOnly="false" path="/bbc">
    <Realm className="com.bbc.tomcat.BBCSecurityRealm"/>
</Context>

After adding this file, I get the following error when Tomcat starts up"

添加此文件后,Tomcat启动时出现以下错误”

ERROR ecmdefault util.digester.Digester 18:37:14,477 localhost-startStop-1 : Parse Fatal Error at line 1 column 1: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1427)

采纳答案by A4L

Your xml file has some invisible chars (most likely the BOM) at the start (before <?xml version="1.0" encoding="UTF-8"?>) which is not allowed in xml. you could view it using a hex editor. Simplest way to fix it is to create an empty text file and copy the content into it, change the extension to xml.

您的 xml 文件在开头(之前<?xml version="1.0" encoding="UTF-8"?>)有一些不可见的字符(很可能是 BOM),这在 xml 中是不允许的。您可以使用十六进制编辑器查看它。修复它的最简单方法是创建一个空文本文件并将内容复制到其中,将扩展名更改为 xml。

Check this answerfor further help.

检查此答案以获得进一步帮助。

From http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

来自http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

UTF8 file are a special case because it is not recommended to add a BOM to them because it can break other tools like Java. In fact, Java assumes the UTF8 don't have a BOM so if the BOM is present it won't be discarded and it will be seen as data.

UTF8 文件是一种特殊情况,因为不建议向其中添加 BOM,因为它会破坏其他工具,例如 Java。事实上,Java 假设 UTF8 没有 BOM,所以如果 BOM 存在,它不会被丢弃,它将被视为数据。

回答by rslj

I had a similar issue but this reference linkprovided the solution spot on.

我有一个类似的问题,但这个参考链接提供了解决方案。

Quote from the article.

引自文章。

Any characters before the start of the XML content will cause above error.

XML 内容开始之前的任何字符都会导致上述错误。

org.xml.sax.SAXParseException: Content is not allowed in prolog error message.