java – com. w3c. dom. Document does not have xml version =“1.0”encoding =“UTF-8”standalone =“no”?>
•
Java
I'm using the following code to create com. Com from string w3c. dom. Document:
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 this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><a><b id="5"/></a>
Everything is OK, but I don't want XML to have Declaration, because I have to sign with my private key and embed it in the soap envelope
Solution
You can use the transformer and set outputkeys OMIT_ XML_ Setting the declaration property 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));
Note that you can also:
>If you don't need a document, use streamsource instead of domsource to provide the string directly to the converter. > If you want to output a document, use domresult instead of streamresult
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码