问题描述
我正在用Java编写一个程序,它使用一个自定义的XML文件并对其进行解析。我正在使用XML文件进行存储。我在Eclipse中收到以下错误。 [致命错误]:1:1:prolog中不允许内容。
org.xml.sax.SAXParseException:prolog中不允许内容。
在com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
在com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl。 parse(DocumentBuilderImpl.java:283)
在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>信息科学与技术< / name>
< degree> B.S.< / degree>
< option>信息系统:设计和开发选项< / option>
< requirements>
< firstlevel type =General_Educationcredits =45>
< component type =Writing_Speaking> GWS< / component>
< component type =Quantification> GQ< / component>
程序能够读取XML文件,但是当我调用 DocumentBuilder .parse(XMLFile)
获得解析的 org.w3c.dom.Document
,我收到上面的错误。
在我看来,我的XML文件的序言中没有内容。我不知道是什么错了。请帮忙。谢谢。
请检查xml文件是否有这样的垃圾字符 。如果存在,请使用以下语法删除它。
String XString = writer.toString();
XString = XString.replaceAll([^ \\ x20-\\\x7e],);
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.
[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 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.
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.
Please check the xml file whether it has any junk character like this �.If exists,please use the following syntax to remove that.
String XString = writer.toString();
XString = XString.replaceAll("[^\\x20-\\x7e]", "");
这篇关于Java解析XML文档给出了“prolog中不允许的内容”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!