eri*_*cso 49 java xml eclipse xml-parsing
我正在用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 )
Run Code Online (Sandbox Code Playgroud)
包含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>
Run Code Online (Sandbox Code Playgroud)
该程序能够读取XML文件,但是当我调用DocumentBuilder.parse(XMLFile)
解析时org.w3c.dom.Document
,我得到上面的错误.
在我看来,我的XML文件的prolog中包含无效内容.我无法弄清楚出了什么问题.请帮忙.谢谢.
小智 19
请检查xml文件是否有任何像这样的垃圾字符 .如果存在,请使用以下语法删除它.
String XString = writer.toString();
XString = XString.replaceAll("[^\\x20-\\x7e]", "");
Run Code Online (Sandbox Code Playgroud)
该文档对我来说看起来不错,但我怀疑它包含不可见的字符。在十六进制编辑器中打开它,检查第一个“<”之前是否确实没有任何内容。确保 XML 标头中的空格是空格。也许删除“?>”之前的空格。检查使用了哪些换行符。
确保文档是正确的 UTF-8。一些 Windows 编辑器将文档保存为 UTF-16(即每隔一个字节为 0)。
归档时间: |
|
查看次数: |
294382 次 |
最近记录: |