I searched on google first and I found many result about how to parse with xpath a xml document. I have parse it but a want to convert a NODELIST in String and I have created a method for it:
private String processResult(Document responseDocument) throws XPathExpressionException, TransformerException {
NodeList soaphead = responseDocument.getElementsByTagName("xmlTagToTrasform");
StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(soaphead.item(0)), new StreamResult(sw));
String result = sw.toString();
return result;
}
This method works perfectly but the transformer adds an <?xml version="1.0" encoding="UTF-8"?>
in the header of the result, and I don't want that. This is the result of the method:
<?xml version="1.0" encoding="UTF-8"?>
<xmlTagToTrasform>
<xmlTagToTrasform2>
.
.
.
.
</xmlTagToTrasform2>
</xmlTagToTrasform>
You can configure the transformer not to output the XML declaration, before you call transform
:
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
See more on this question at Stackoverflow