I've been using the Java APIs to parse an XML file so I could add, remove, or update elements/attributes. Everything works the way I want, except that the Transformer
object I'm using adds <?xml version="1.0" encoding="UTF-8"?>
to the beginning of the XML file. I was wondering if there is a way to suppress this.
P.S. I also noticed that this top-voted answer mentioned that we might be able to supress it.
DOMSource source = new DOMSource(document);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
FileOutputStream fout = new FileOutputStream(new File(outputFile));
StreamResult result = new StreamResult(fout);
transformer.transform(source, result);
fout.close();
The original document does not contain <?xml version="1.0" encoding="UTF-8"?>