aspose file tools*
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
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM to string" Watch "DOM to string" New topic
Author

DOM to string

Aaron Webb
Greenhorn

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

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.io.StringWriter;
import java.utils.Properties
import javax.xml.transform.stream.StreamResult;
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;
try{
t = tf.newTransformer();
t.setOutputProperties(oprops);
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 java.io.StringWriter;
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();
if(doc!=null){
OutputFormat opfrmt = new OutputFormat(doc.getOwnerDocument(), "UTF-8", true);
opfrmt.setIndenting(true);
opfrmt.setPreserveSpace(false);
opfrmt.setLineWidth(500);
XMLSerializer serial = new XMLSerializer(stringOut, opfrmt);
try{
serial.asDOMSerializer();
serial.serialize( doc );
xmlString = stringOut.toString();
} catch(java.io.IOException ioe){
xmlString=null;
}
} 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,
Anthony
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.
Header.appendChild(Function);
Header.appendChild(RequestTimestamp);
Header.appendChild(ResponseTimestamp);
Header.appendChild(RequestorInfo);
Header.appendChild(Status);
root.appendChild(Header);
root.appendChild(Requestd);
root.appendChild(RespData);
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
Greenhorn

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: http://www.javaranch.com/ubb/Forum31/HTML/001363.html
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM to string