List of utility methods to do XML Document to String
String | xmlToString(Document xml) Method to transform an XML document into a pretty-formatted string. Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); return out.toString(); |
String | XMLToString(Document XMLDocument) Converts DOM document to a string String xmlString = null; try { StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.transform(new DOMSource(XMLDocument), result); xmlString = sw.toString(); sw.close(); ... |
String | xmltoString(final Document document) xmlto String StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(document.getDocumentElement()), streamResult); ... |
StringBuffer | xmlToStringBuffer(Document doc, int identAmount) xml To String Buffer final TransformerFactory transfac = TransformerFactory.newInstance(); final Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(identAmount)); final StringWriter sw = new StringWriter(); final StreamResult result = new StreamResult(sw); final DOMSource source = new DOMSource(doc); ... |