I use the following method to transform an xml node to String (commonly found on the web):
String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
throw ...
}
return sw.toString();
}
The Nodes transformed to String are not whole xml documents but just parts of a bigger XML.
The problem is that after transformed to String the root element of the Node has an xmlns added to it, which causes problems.
This is the String returned by nodeToString :
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
...
</header>
<payload>
...
</payload>
</doc>
Worst part is that this does not happen on my machine and the Test environment but only on the UAT and I struggle to find the difference between the environments.
Does anybody bumped into this problem and know what causes it?
Edit:
The original document looks like this:
<docs>
<doc>
<header>
...
</header>
<payload>
...
</payload>
</doc>
<doc>
<header>
...
</header>
<payload>
...
</payload>
</doc>
// more <doc>s
<docs>
It's splitted with XPath to Nodes (each Node is one element) then some business logic is applied (some nodes are removed, some are grouped differently) and at the end I have to turn it back to String.