This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes DOM to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM to string" Watch "DOM to string" New topic

DOM to string

Aaron Webb

Joined: Aug 16, 2001
Posts: 17
How can I convert a DOM tree to a string?
Anthony Ikeda

Joined: Jan 23, 2001
Posts: 6
This depends on what XML package you wish to use (JAXP, Xalan/Xerces, etc) and which language (Java, C++, C#, etc).
For JAXP the easiest way is to use the Xalan transformer to transform the XML to text:
import org.w3c.dom.Node;
import java.utils.Properties
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
public static String serializeDoc(Node doc){
StringWriter outText = new StringWriter();
StreamResult sr = new StreamResult(outText);
Properties oprops = new Properties();
oprops.put(OutputKeys.METHOD, "html");
oprops.put("indent-amount", "4");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = null;
t = tf.newTransformer();
t.transform(new DOMSource(doc),sr);
} catch(Exception e){}
return outText.toString();
as you can see, it just uses the Transformer to transform an XML document without a stylesheet, thus you get no transformation except the string. With Xerces, however, you have whats known as Serializers:
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.XMLSerializer;
public static String serializeDoc(Element doc){
String xmlString = new String();
StringWriter stringOut = new StringWriter();
OutputFormat opfrmt = new OutputFormat(doc.getOwnerDocument(), "UTF-8", true);
XMLSerializer serial = new XMLSerializer(stringOut, opfrmt);
serial.serialize( doc );
xmlString = stringOut.toString();
} catch( ioe){
} else xmlString=null;
return xmlString;
as you can see this uses custom classes to "serialize" the document leaving you with a string representation of the xml.
Good luck,
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
In JAXP after I constructed my DOM tree to send out I just called the .toString() method on it and that was all.
String Response = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
Response = Response + "<!-- This document is generated by a MQSeries Workflow Version 3.2.2 server -->";
Response = Response + root.toString();

Sun Certified Programmer on the Java 2 Platform
Glen Jansen

Joined: Apr 07, 2001
Posts: 14
Have you thought about using an XSL transformation to convert the DOM object to a string. I posted a way to do this in a previous thread:
Basically, the XSL stylesheet is designed to output the text only. Then you can design a method that when passed a stylesheet and a document, it returns the result as a string.
Hope this helps.
Glen Jansen
Java/XML Cert in Training.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: DOM to string
Similar Threads
Pretty printing XML
DOM non validation parsing
how to use Xalan with Weblogic as a web server
Convert String to XML, then search for text
Converting Document to String