I have following class with two methods. openInputStream
creates an input stream, which is subsequently fed into readDocument
to create XML document from that stream.
public class XmlReader {
protected InputStream openInputStream(final String path)
throws IOException {
final InputStream inputStream;
inputStream = IOUtils.toInputStream(path, "UTF-8");
return inputStream;
}
protected Document readDocument(final InputStream stream)
throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilderFactory dbfac =
DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
return docBuilder.parse(stream);
}
}
Then I have the following test:
public class XmlReaderTests {
@Test
public void parsingOfMapFiles() throws IOException,
SAXException, ParserConfigurationException {
final String[] dirs = new String[] {
"01_Phoenix1",
"02_Phoenix2",
"03_Guadalupe",
"04_SanDiego_MiramarRoad",
"05_Berlin_Alexanderplatz",
"06_Portland_ForestAvenue",
"07_Hartford_LafayetteHudsonStreet",
"08_FortWorth",
"09_Charleston_CalhounStreetMeetingStreet",
"10_LosAngeles_PershingSquare",
"11_Uelzen"
};
for (final String dir : dirs) {
final XmlReader objectUnderTest = new XmlReader();
final String path =
String.format("src/test/resources/mc/E-2015-10-13_1/%s/map.osm",
dir);
final File file = new File(path);
Assert.assertTrue(file.canRead());
final InputStream inputStream =
objectUnderTest.openInputStream(file.getAbsolutePath());
final Document doc = objectUnderTest.readDocument(inputStream);
Assert.assertNotNull(doc);
}
}
}
readDocument
throws the exception
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at XmlReader.readDocument(XmlReader.java:26)
at XmlReaderTests.parsingOfMapFiles(XmlReaderTests.java:40)
You can find the source code and the XML files I'm trying to read here.
How can I fix this error?
Update 1: Changing openStream
to
protected InputStream openInputStream(final String path)
throws IOException {
return new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
}
or
protected InputStream openInputStream(final String path)
throws IOException {
return new BOMInputStream(IOUtils.toInputStream(path));
}
didn't help.
Update 2:
If I change the openInputStream
method like that
protected InputStream openInputStream(final String path)
throws IOException {
BOMInputStream inputStream = new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
System.out.println("BOM: " + inputStream.hasBOM());
return inputStream;
}
I get this output:
BOM: false
[Fatal Error] :1:1: Content is not allowed in prolog.