Java 致命错误:1:1:prolog中不允许包含内容。org.xml.sax.SAXParseException

Java 致命错误:1:1:prolog中不允许包含内容。org.xml.sax.SAXParseException,java,xml,xsd,xml-parsing,Java,Xml,Xsd,Xml Parsing,我试图用Java读取一个XML文件,然后将其与XML模式进行比较,但我无法克服此错误: [致命错误]:1:1:prolog中不允许包含内容。 org.xml.sax.SAXParseException;行号:1;列数:1;prolog中不允许包含内容 这是文件读取的开始 try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBu

我试图用Java读取一个XML文件,然后将其与XML模式进行比较,但我无法克服此错误:

[致命错误]:1:1:prolog中不允许包含内容。 org.xml.sax.SAXParseException;行号:1;列数:1;prolog中不允许包含内容

这是文件读取的开始

try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();          
        Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml"))); // ERROR OCCURS HERE
我通过十六进制编辑器扫描了我的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>

人工智能
星期三
09-11
哈茨里格洛迪
约束满足问题
星期一
19-21
Web中的知识表示
星期五
15-17
哈茨里格洛迪
人工智能
星期一
19-21
人工智能编程
星期一
11-13
程序设计导论
星期三
15-17
帕帕佐普洛斯

您可能有一个带有字节顺序标记(BOM)的UTF-8文件。它对大多数编辑器都是不可见的,但可能会干扰解析器。尝试在没有BOM的情况下转换为UTF-8。

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"));