Code example for StringWriter

Methods: close, flush, toString

0
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(xmlDoc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        writer.flush();
        String xml = writer.toString();
        writer.close();
 
        //manually generate xmlns for Autodiscover and Response element, this works 
        //for testexchangeconnectivity.com, but iOS and Android don't like Response's xmlns 
        //        StringBuilder str = new StringBuilder(); 
        //        str.append("<?xml version=\"1.0\"?>\n"); 
        //        str.append("<Autodiscover xmlns:xsd=\"").append(XSD_NS).append("\""); 
        //        str.append(" xmlns:xsi=\"").append(XSI_NS).append("\""); 
        //        str.append(" xmlns=\"").append(NS).append("\">\n"); 
        //        int respIndex = xml.indexOf("<Response>"); 
        //        str.append("<Response xmlns=\"").append(NS_MOBILE).append("\">");