Com.w3c.dom.Document without <? xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
I am creating com.w3c.dom.Documentfrom Stringusing this code:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader("<a><b id="5"/></a>")));
When I System.out.println(xmlToString(document)), I get the following:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><a><b id="5"/></a>
Everything is fine, but I do not want the XML to have an declaration <?xml version="1.0" encoding="UTF-8" standalone="no"?>, because I need to sign the private key and paste it into the soap envelope.
+5
1 answer
You can use Transformerand install OutputKeys.OMIT_XML_DECLARATIONup to "yes":
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter sw = new StringWriter();
t.transform(new DOMSource(doc), new StreamResult(sw));
Please note that you can also:
- Use
StreamSourceinsteadDOMSourcefor direct transmissionStringto the transformer, if you really do not use it neededDocument. DOMResultStreamResult,Document.
+11