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