Help with transforming dom object
Hello,
I have a bit of code that reads in XML from a string and then modifies the document and then converts it back into a string. The problem is whenever I use the Transformer, the resulting xml is very different from what I originally had. Information like namespaces are no longer there.
I'm inputing:
<test>
<body xmlns="http://www.someplace.com/namespace\">a value goes in here</body>
</test>
And getting back:
<?xml version="1.0" encoding="UTF-8"?><test>
<body>a value goes in here</body>
</test>
Basically I need them to be the same. I've pasted a simple example of what I'm doing below. All this does is read in the xml and then output it back into a String. Any help would be appreciated. Thanks.
publicstaticvoid main(String[] args)throws Exception{
String xml ="<test>\n" +
"\t<body xmlns=\"http://www.someplace.com/namespace\\\">a value goes in here</body>\n" +
"</test>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
Transformer trans = TransformerFactory.newInstance().newTransformer();
StringWriter writer =new StringWriter();
trans.transform(new DOMSource(doc),new StreamResult(writer));
System.out.println(writer.getBuffer().toString());
}

