Document to String?

Posted by Nate on Stack Overflow See other posts from Stack Overflow or by Nate
Published on 2010-04-02T15:17:44Z Indexed on 2010/04/02 15:23 UTC
Read the original article Hit count: 246

Filed under:

I've been fiddling with this for over twenty minutes and my Google-foo is failing me.

Let's say I have an XML Document created in Java (org.w3c.dom.Document):

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document document = docBuilder.newDocument();

Element rootElement = document.createElement("RootElement");
Element childElement = document.createElement("ChildElement");
childElement.appendChild(document.createTextNode("Child Text"));
rootElement.appendChild(childElement);

document.appendChild(rootElement);

String documentConvertedToString = "?" // <---- How?

How do I convert the document object into a text string?

© Stack Overflow or respective owner

Related posts about java