Java 序言中不能有内容

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

Content is not allowed in prolog

javaxmlparsingutf

提问by Senthil Kumar

i'm trying to convert xmlto html using xslt. Am using java.xml.transformto do this in java. it was working fine until i bumped into some xml. it said the following error.

我正在尝试xml使用 .html转换为 html xslt。我正在使用java.xml.transformJava 来执行此操作。它工作正常,直到我碰到了一些xml。它说以下错误。

[Fatal Error] :1:1: Content is not allowed in prolog. 
    javax.xml.transform.TransformerConfigurationException: 
    javax.xml.transform.TransformerConfigurationException: 
    javax.xml.transform.TransformerException: 
    org.xml.sax.SAXParseException: Content is not allowed in prolog.

so i made sure there is no character before the xml declaration. i even took care of BOM using the solution http://forums.sun.com/thread.jspa?messageID=10324562#10324562

所以我确保在 xml 声明之前没有字符。我什至使用解决方案处理 BOM http://forums.sun.com/thread.jspa?messageID=10324562#10324562

STILL no luck and it happens only for one xml. i even opened the xml in editor and saved it in a file with utf-8encoding. this is driving me crazy. any idea?

仍然没有运气,它只发生在一个 xml 中。我什至在编辑器中打开了 xml 并将其保存在一个带有utf-8编码的文件中。这真让我抓狂。任何的想法?

UPDATE: You get this error when you have given the wrong path for the xslfile and a file not found exception happens. (this was my case. it might help somebody. thanks for your responses)

更新:当您为xsl文件提供了错误的路径并且发生文件未找到异常时,您会收到此错误。(这是我的情况。它可能对某人有所帮助。感谢您的回复)

回答by Jesper

This kind of thing can happen if you have an UTF-8 file with a BOM, and if you use an XML parser that isn't aware of it. Save the XML file as UTF-8 without BOM.

如果您有一个带有BOM的 UTF-8 文件,并且如果您使用不知道它的 XML 解析器,则可能会发生这种情况。将 XML 文件另存为不带 BOM 的 UTF-8。

回答by Noel M

Do you have a header in your file? Something like:

你的文件中有标题吗?就像是:

<?xml version="1.0" encoding="utf-8"?>

<?xml version="1.0" encoding="utf-8"?>

That should be at the start of the first line. Unfortunately I can't see your XML file as that URL is blocked from where I am.

那应该在第一行的开头。不幸的是,我无法看到您的 XML 文件,因为该 URL 已从我所在的位置被阻止。