I have an xml document object that I need to convert into a string.
Is there as simple way to do this?
Here's some quick code I pulled out of a library I had nearby. Might wanna dress it up, but it works:
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public String TransformDocumentToString(Document doc)
{
DOMSource dom = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(dom, result);
return writer.toString();
}
edit: as commentor noticed earlier, i had a syntax error. had to pull out some sensitive lines so I wouldn't get canned and put them back in the wrong order. thanks! ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With