See the following attached function, we've using it for a while now,
should do what you require (doesn't pretty print).
------------------------------------------------------------------------
----
/** Returns source underlying xml document. This a fail quick
implementation, if the document is not in a
* ready state then this method will throw an exception.
* @param node the org.w3c.dom.Node to be serialised.
* @param omit True if XML declaration should be ommited.
*/
public static final java.lang.String getNodeXML(org.w3c.dom.Node
node, boolean omit) throws com.synergy.exceptions.ChainedException {
String xml = "";
try {
if (node != null) {
//Create a StringWriter object to pass to the
FormatterToXML instance
java.io.StringWriter writer = new
java.io.StringWriter();
//Instruct the output formatter whether we want the XML
declartions or not
OutputFormat format = new OutputFormat((node instanceof
org.w3c.dom.Document) ? (org.w3c.dom.Document)node :
node.getOwnerDocument());
format.setOmitXMLDeclaration(omit);
//Uses the Xerces Serializer implementation
XMLSerializer
XMLSerializer serializer = new XMLSerializer(writer,
format);
//Determine which version of the serialise method to use
if (node instanceof org.w3c.dom.Document)
serializer.serialize((org.w3c.dom.Document)node);
else
serializer.serialize((org.w3c.dom.Element)node);
//Convert the contents of the StringWriter into a String
xml = writer.toString();
}
} catch (Exception e) {
throw new ChainedException(e, Locale.getDefault());
}
return xml;
}
--------------------------------------------------------
Anthony Dodd
Synergy Financial Systems Limited
Synergy House, Highfields Science Park
Nottingham NG7 2RF
United Kingdom
t: +44 (0)115 9677990
f: +44 (0)115 9677933
e: [EMAIL PROTECTED]
w: www.synergy-fs.com
-----Original Message-----
From: Johannes Kjos [mailto:[EMAIL PROTECTED]
Sent: 09 October 2003 12:39
To: '[EMAIL PROTECTED]'
Subject: Method.TEXT
Hello.
Simple problem: How do I make a String from Document? (I'm using the
xalan-j_2_5_1 distribution)
My suggestion:
----------
Serializer ser = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
WriterToUTF8Buffered writer = null;
DOMSerializer domser = null;
Properties props = null;
try{
writer = new WriterToUTF8Buffered(os);
props =
OutputPropertiesFactory.getDefaultMethodProperties(Method.TEXT);
ser = SerializerFactory.getSerializer(props);
ser.setWriter(writer);
domser = (DOMSerializer) ser.asDOMSerializer();
domser.serialize(doc);
writer.close();
return os.toString();
}catch(...
-----------
This works if I use Method.XML - but this adds the standard XML
starttagline
in my document (and I don't want that), that's why I use Method.TEXT.
Problem is: Nothing is written, only empty String with the right
size...There is something here I have missed, isn't it?
Johannes