I have some code which reads in an XML file, formats it and then outputs it again to the same file. However if there is no encoding defined, the output XML has UTF-8 defined.
For example:
<?xml version="1.0"?>
becomes:
<?xml version="1.0" encoding="UTF-8"?>
I was wondering if there was any way to preserve whatever encoding (or lack of encoding) that was there before?
Here is my current code:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document document = docBuilder.parse(file);
OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
//custom method to write file
writeFile(filePath, out.toString());
Any help is appreciated. Thanks.