java 致命错误:1:1:序言中不允许出现内容。org.xml.sax.SAXParseException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47358087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal Error :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException
提问by ceid-vg
I am trying to read an XML file in Java and then compare it against its XML Schema but I can't get past this error :
我正在尝试用 Java 读取 XML 文件,然后将其与其 XML 架构进行比较,但我无法克服此错误:
[Fatal Error] :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
[致命错误] :1:1: prolog 中不允许出现内容。org.xml.sax.SAXParseException; 行号:1;列数:1;序言中不能有内容。
This is the start of the file reading
这是文件读取的开始
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml"))); // ERROR OCCURS HERE
I scanned my XML through HEX Editors but I did not find any weird characters inside, so I dont know where the problem is
我通过 HEX 编辑器扫描了我的 XML 但我没有在里面找到任何奇怪的字符,所以我不知道问题出在哪里
myfile.xml
我的文件.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>
回答by kjhughes
StringReader("myfile.xml")
takes a string argument that must be XML, not a filename. The parser is reading the string literal, myfile.xml
, (not the file contents of myfile.xml
) and failing immediately because an XML document may not begin with an m
character.
StringReader("myfile.xml")
接受一个必须是 XML 的字符串参数,而不是文件名。解析器正在读取字符串文字 , myfile.xml
(不是 的文件内容myfile.xml
)并立即失败,因为 XML 文档可能不是以m
字符开头。
Change
改变
Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml")));
to
到
Document doc = dBuilder.parse(new InputSource("myfile.xml"));
回答by Piotr Wilkin
You probably have an UTF-8 file with a byte-order marker (BOM). It'll be invisible to most editors, but might mess with the parser. Try converting to UTF-8 without BOM.
您可能有一个带有字节顺序标记 (BOM) 的 UTF-8 文件。大多数编辑器都看不到它,但可能会弄乱解析器。尝试在没有 BOM 的情况下转换为 UTF-8。