Java 解析 XML 文档给出“序言中不允许的内容”。错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2599919/
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
Java parsing XML document gives "Content not allowed in prolog." error
提问by ericso
I am writing a program in Java that takes a custom XML file and parses it. I'm using the XML file for storage. I am getting the following error in Eclipse.
我正在用 Java 编写一个程序,它接受一个自定义的 XML 文件并解析它。我正在使用 XML 文件进行存储。我在 Eclipse 中收到以下错误。
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283 )
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at me.ericso.psusoc.RequirementSatisfier.parseXML(RequirementSatisfier.java:61)
at me.ericso.psusoc.RequirementSatisfier.getCourses(RequirementSatisfier.java:35)
at me.ericso.psusoc.programs.RequirementSatisfierProgram.main(RequirementSatisfierProgram.java:23 )
The beginning of the XML file is included:
XML 文件的开头包括:
<?xml version="1.0" ?>
<PSU>
<Major id="IST">
<name>Information Science and Technology</name>
<degree>B.S.</degree>
<option> Information Systems: Design and Development Option</option>
<requirements>
<firstlevel type="General_Education" credits="45">
<component type="Writing_Speaking">GWS</component>
<component type="Quantification">GQ</component>
The program is able to read in the XML file but when I call DocumentBuilder.parse(XMLFile)
to get a parsed org.w3c.dom.Document
, I get the error above.
该程序能够读取 XML 文件,但是当我调用DocumentBuilder.parse(XMLFile)
以获取 parsed 时org.w3c.dom.Document
,出现上述错误。
It doesn't seem to me that I have invalid content in the prolog of my XML file. I can't figure out what is wrong. Please help. Thanks.
在我看来,我的 XML 文件的序言中没有无效内容。我无法弄清楚出了什么问题。请帮忙。谢谢。
回答by Aaron Digulla
The document looks fine to me but I suspect that it contains invisible characters. Open it in a hex editor to check that there really isn't anything before the very first "<". Make sure the spaces in the XML header are spaces. Maybe delete the space before "?>". Check which line breaks are used.
该文档对我来说看起来不错,但我怀疑它包含不可见的字符。在十六进制编辑器中打开它以检查在第一个“<”之前是否真的没有任何内容。确保 XML 标头中的空格是空格。也许删除“?>”之前的空格。检查使用了哪些换行符。
Make sure the document is proper UTF-8. Some windows editors save the document as UTF-16 (i.e. every second byte is 0).
确保文档是正确的 UTF-8。一些 Windows 编辑器将文档保存为 UTF-16(即每第二个字节为 0)。
回答by Ben J
Make sure there's no hidden whitespace at the start of your XML file. Also maybe include encoding="UTF-8" (or 16? No clue) in the node.
确保 XML 文件的开头没有隐藏的空格。也可能在节点中包含 encoding="UTF-8"(或 16?无线索)。
回答by Drew Johnson
If you're able to control the xml file, try adding a bit more information to the beginning of the file:
如果您能够控制 xml 文件,请尝试在文件开头添加更多信息:
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
回答by Alfabravo
Check any syntax problem in the XMl file. I've found this error when working on xsl/xsp with Cocoon and I define a variable using a non-existing node or something like that. Check the whole XML.
检查 XML 文件中的任何语法问题。我在使用 Cocoon 处理 xsl/xsp 时发现了这个错误,我使用不存在的节点或类似的东西定义了一个变量。检查整个 XML。
回答by Gopal
Please check the xml file whether it has any junk character like this ?.If exists,please use the following syntax to remove that.
请检查xml文件是否有这样的垃圾字符?。如果存在,请使用以下语法将其删除。
String XString = writer.toString();
XString = XString.replaceAll("[^\x20-\x7e]", "");
回答by spark07
I assume you have proper xml encoding and matching with Schema.
我假设您有正确的 xml 编码并与 Schema 匹配。
If you still get this error, check code that unmarshalls the xml and input type you have used. Because XML documents declare their own encoding, it is preferable to create a StreamSource object from an InputStream instead of from a Reader, so that XML processor can correctly handle the declared encoding [Ref Book: Java in A Nutshell ]
如果您仍然遇到此错误,请检查对您使用的 xml 和输入类型进行解组的代码。因为 XML 文档声明了它们自己的编码,所以最好从 InputStream 而不是从 Reader 创建 StreamSource 对象,以便 XML 处理器可以正确处理声明的编码 [参考书:Java in A Nutshell]
Hope this helps!
希望这可以帮助!
回答by Java_Alert
I think this is also a solution of this problem.
我认为这也是解决这个问题的方法。
Change your document type from 'Encode in UTF-8'To 'Encode in UTF-8 without BOM'
将您的文档类型从“以 UTF-8 编码”更改为“以不带 BOM 的 UTF-8 编码”
I got resolved my problem by doing same changes.
通过进行相同的更改,我解决了我的问题。
回答by Amit Agarwal
You are not providing the correct address for the file. You need to provide an address such as C:/Users/xyz/Desktop/myfile.xml
您没有为文件提供正确的地址。您需要提供一个地址,例如C:/Users/xyz/Desktop/myfile.xml