【发布时间】:2011-02-05 16:39:55
【问题描述】:
我正在用 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 )
包含 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>
该程序能够读取 XML 文件,但是当我调用 DocumentBuilder.parse(XMLFile) 以获取已解析的 org.w3c.dom.Document 时,出现上述错误。
在我看来,我的 XML 文件的序言中没有无效内容。我不知道出了什么问题。请帮忙。谢谢。
【问题讨论】:
-
我发现了我的错误。我正在阅读文件所在的文件夹,而不是文件本身。显然,如果您将文件夹作为文件读取并在其上调用 File.exists(),它仍然会返回 true。愚蠢的我......感谢所有的帮助。
-
在stackoverflow.com/questions/3665554/…查看我的答案,或者直接查看这个链接mark.koli.ch/2009/02/…
-
不知道它是否会帮助任何人,但我在尝试使用 flavorDimensions 并将 drawable-xhdpi 置于我的风格中的 res 下时遇到了这个错误。一旦我将其更改为可绘制.. 全部固定
标签: java xml eclipse xml-parsing