Code example for StringWriter

Methods: toString

     * Serialize to XML String 
     * @param document The DOM document 
     * @return The XML String 
    public static String serialize(Document document) {
        StringWriter writer = new StringWriter();
        try { 
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(writer);
            transformer.transform(domSource, streamResult); 
        } catch (TransformerException e) {
            throw new RuntimeException(
                    "Error when serializing XML document.", e);
        return writer.toString();
     * Parse an XML file to DOM 
     * @return null if an error occurs during parsing. 
Connect your IDE to all the code out there  Get Codota for Java