I have looked a lot for a solution, the only suggestion I've seen is that there may be illegal characters before the XML declaration, however this isn't my issue.
The following code is used to write an XML file, when I read it back I get the exception.
Write file code:
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
OMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS","3.0");
LSSerializer ser = implLS.createLSSerializer();
xmlString = ser.writeToString(doc);
String fileName = "myFile";
File file = new File(fileName);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(xmlString);
Read file code:
File file = new File(fileName);
Document doc = builder.parse(file);
This is how the XML file looks:
<?xml version="1.0" encoding="UTF-16"?>
If I remove this part: ' encoding="UTF-16"' then I don't get the exception and it works correctly... I read this code in the book Big Java, apparantly Documents can't read XML files they themselves wrote?
No comments:
Post a Comment