我正在尝试读取Java中的XML文件,然后将其与其XML Schema进行比较,但我无法克服此错误:
[致命错误]:1:1:序言中不允许包含内容。org.xml.sax.SAXParseException; lineNumber:1;columnNumber:1;序言中不能有内容。
这是文件读取的开始
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml"))); // ERROR OCCURS HERE
我通过HEX编辑器扫描了XML,但是内部没有发现任何奇怪的字符,因此我不知道问题出在哪里
myfile.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Schedule xmlns ="schedule"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schedule.xsd">
<Lesson>
<Title>Artificial Intelligence</Title>
<Lecture Classroom="BA">
<Day>Wednesday</Day>
<Time>09-11</Time>
</Lecture>
<Professor>Hatzilygeroudis</Professor>
</Lesson>
<Lesson>
<Title>Constraint Satisfaction Problems</Title>
<Lecture Classroom="B3">
<Day>Monday</Day>
<Time>19-21</Time>
</Lecture>
</Lesson>
<Lesson>
<Title>Knowledge Representation in Web</Title>
<Lecture Classroom="P200">
<Day>Friday</Day>
<Time>15-17</Time>
</Lecture>
<Professor>Hatzilygeroudis</Professor>
</Lesson>
<Lesson>
<Title>Artificial Intelligence</Title>
<Lecture>
<Day>Monday</Day>
<Time>19-21</Time>
</Lecture>
</Lesson>
<Lesson>
<Title>AI Programming</Title>
<Lecture Classroom="B3">
<Day>Monday</Day>
<Time>11-13</Time>
</Lecture>
</Lesson>
<Lesson>
<Title>Introduction to Procedural Programming</Title>
<Lecture Classroom="P200">
<Day>Wednesday</Day>
<Time>15-17</Time>
</Lecture>
<Professor>Papadopoulos</Professor>
</Lesson>
</Schedule>
StringReader("myfile.xml")
采用必须为XML而不是文件名的字符串参数。解析器正在读取字符串文字(myfile.xml
,而不是的文件内容myfile.xml
)并立即失败,因为XML文档可能不是以m
字符开头。
更改
Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml")));
至
Document doc = dBuilder.parse(new InputSource("myfile.xml"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句