I'm using Primefaces <p:fileUpload>
component for file selection by users.
In that, I'm using the fileUploadListener
attribute that uses a custom method to read XML files and parse them using DOMParser
. My file reading and parsing code boils down to this much:
InputStream inputStream = new FileInputStream("C:\\test\\test_ansi.xml");
Reader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
InputSource is = new InputSource(new BufferedReader(inputStreamReader));
DOMParser parser = new DOMParser();
parser.parse(is);
Document doc = parser.getDocument();
I have a need to convert all user-selected XMLs to UTF-8 before processing them further. If I try the above code with a file encoded as ANSI, it works fine. But if the file is encoded as UTF-8, I'm getting the below error:
oracle.xml.parser.v2.XMLParseException; lineNumber: 1; columnNumber: 1; Start of root element expected.
at oracle.xml.parser.v2.XMLError.flushErrors(XMLError.java:233)
at oracle.xml.parser.v2.XMLError.error(XMLError.java:133)
at oracle.xml.parser.v2.XMLError.error(XMLError.java:171)
at oracle.xml.parser.v2.NonValidatingParser.parseRootElement(NonValidatingParser.java:280)
at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:241)
at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:124)
For sample UTF-8 files, I'm just saving the file with encoding as UTF-8 in Notepad. Can anyone please help me understand what's going wrong?