20080219 Tuesday February 19, 2008

Converting a DOM Document to a String

Here is a simple Java method for converting a DOM object into a String. Hope someone finds it useful:

 private String domToString(Document doc) throws TransformerException {
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer t = tf.newTransformer();
  StringWriter str = new StringWriter();
  StreamResult result = new StreamResult(str);
  DOMSource source = new DOMSource(doc);
  t.transform(source, result);
  return str.toString();
 }

Posted by rickg ( Feb 19 2008, 08:04:39 PM PST ) Permalink Comments [0]
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed