I’ve been using the classes from Xerces and Xalan for serializing XML documents and nodes to strings. I hated using specific implementations of what I thought should be standard functionality within
javax.xml.*, but I couldn’t find an alternative.
Plus, the serialization classes change depending on the version of Xerces… nice. I ran into this piece of code watching a presentation on effective API design from Google and Joshua Bloch. The individual slide doesn’t reference a source, however.
Here it is in (tested!) code:
StringWriter out = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(node), new StreamResult(out));
System.out.println("out=" + out.toString());