Is there a more elegant way to convert an XML Document to a String in Java than this code?

Here is the code currently used.

public String getStringFromDoc(org.w3c.dom.Document doc)    {
           DOMSource domSource = new DOMSource(doc);
           StringWriter writer = new StringWriter();
           StreamResult result = new StreamResult(writer);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
           return writer.toString();
        catch(TransformerException ex)
           return null;


Relies on DOM Level3 Load/Save:

public String getStringFromDoc(org.w3c.dom.Document doc)    {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);   

Need Your Help

Thorough use of 'if' statements or 'try/catch' blocks?

comparison if-statement try-catch

Give me some of your thoughts on which is a better coding practice/makes more efficient code/looks prettier/whatever: Increasing and improving your ability to use if statements to anticipate and c...