This is explained by @MikeSokolov in the first of your links. Quote:
Another thing that often happens is a UTF-8 BOM (byte order mark), which is allowed before the XML declaration can be treated as whitespace if the document is handed as a stream of characters to an XML parser rather than as a stream of bytes.
FileReader
reads the file as a character stream, and to read the file as a byte stream, you should use FileInputStream
instead, as follows:
FileInputStream is = new FileInputStream(xml);
xr.parse(new InputSource(is));
If you examine your text file in a hex editor you will see the UTF-8 BOM at the start (EF BB BF)
and it is this that is causing the problem when using FileReader
.
0
solved java – SAXparserException: content is not allowed in prolog