Reputation: 333
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 :
[Fatal Error] :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
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
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>
Upvotes: 2
Views: 24820
Reputation: 111726
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.
Change
Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml")));
to
Document doc = dBuilder.parse(new InputSource("myfile.xml"));
Upvotes: 7
Reputation: 3501
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.
Upvotes: 2