問題描述
我正在用 Java 編寫一個(gè)程序,它接受一個(gè)自定義 XML 文件并對其進(jìn)行解析.我正在使用 XML 文件進(jìn)行存儲(chǔ).我在 Eclipse 中收到以下錯(cuò)誤.
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 )
包含 XML 文件的開頭:
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>
程序能夠讀取 XML 文件,但是當(dāng)我調(diào)用 DocumentBuilder.parse(XMLFile)
來獲取已解析的 org.w3c.dom.Document
時(shí),我得到上面的錯(cuò)誤.
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 文件的序言中沒有無效內(nèi)容.我不知道出了什么問題.請幫忙.謝謝.
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文件是否有這樣的垃圾字符.如果存在,請使用以下語法刪除.
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]", "");
這篇關(guān)于Java 解析 XML 文檔給出“Prolog 中不允許的內(nèi)容".錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!