• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

DOM to string

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I convert a DOM tree to a string?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic