java - How to fix the error "org.xml.sax.SAXParseException / Content is not allowed in prolog"? -
i have following class 2 methods. openinputstream
creates input stream, subsequently fed readdocument
create xml document 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 have 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" }; (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 exception
org.xml.sax.saxparseexception; linenumber: 1; columnnumber: 1; content not allowed in prolog. @ com.sun.org.apache.xerces.internal.parsers.domparser.parse(domparser.java:257) @ com.sun.org.apache.xerces.internal.jaxp.documentbuilderimpl.parse(documentbuilderimpl.java:347) @ javax.xml.parsers.documentbuilder.parse(documentbuilder.java:121) @ xmlreader.readdocument(xmlreader.java:26) @ xmlreadertests.parsingofmapfiles(xmlreadertests.java:40)
you can find source code , xml files i'm trying read here.
how can fix 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 change openinputstream
method 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 output:
bom: false [fatal error] :1:1: content not allowed in prolog.
in xmlreader.openinputstream
, change
inputstream = ioutils.toinputstream(path, "utf-8");
to
inputstream = new fileinputstream(new file(path));
ioutils.toinputstream
converts given string inputstream, in case string containing path, instead of file itself.
Comments
Post a Comment