Is there any way to tell the Transformer (when serializing an XML document using DOM), to omit the standalone attribute?
Preferably without using a hack, i.e. ommitting the whole XML declaration and then prepending it manually.
My current code:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //Note nothing is changed
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
return result.getWriter().toString();
Current:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<someElement/>
Intended:
<?xml version="1.0" encoding="UTF-8">
<someElement/>
The XML standalone element defines the existence of an externally-defined DTD. In the message tree it is represented by a syntax element with field type XML. standalone. A value of no indicates that this XML document is not standalone, and depends on an externally-defined DTD.
DOM is an acronym stands for Document Object Model. It defines a standard way to access and manipulate documents. The Document Object Model (DOM) is a programming API for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated.
Figured it out..
Instead of changes to the transformer,
I add the following to the document object.
document.setXmlStandalone(true);
document.setXmlStandalone(true/false);
is working OK.
You have to use a combination of:
doc.setXmlStandalone(true);
and
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // this is used to show the standalone tag
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With