List of utility methods to do XML Document to String
String | document2String(Document doc) document String DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domFact.newDocumentBuilder(); 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); ... |
String | document2String(Document doc, boolean prettyPrint) document String StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); if (prettyPrint) { transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); ... |
String | document2String(Node document) document String if (document == null) return null; StringWriter stringWriter = new StringWriter(); try { identityTransformer.transform(new DOMSource(document), new StreamResult(stringWriter)); } catch (TransformerException e) { e.printStackTrace(); return stringWriter.toString(); |
String | document2XmlString(Document xmldoc) document Xml String try { Source src = new DOMSource(xmldoc); ByteArrayOutputStream bout = new ByteArrayOutputStream(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(src, new StreamResult(bout)); return bout.toString("UTF-8"); } catch (Exception e) { ... |
String | documentToString(Document d) Convert an XML DOM Document to a String representation StringWriter s = new 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.transform(new DOMSource(d), new StreamResult(s)); return s.toString(); |
String | documentToString(Document d) document To String Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(d); transformer.transform(source, result); ... |
String | documentToString(Document doc) Transforms the given XML document into its textual representation. StreamResult result = new StreamResult(new StringWriter()); transform(doc, result); String xmlString = result.getWriter().toString(); return xmlString; |
String | documentToString(Document doc) document To String TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String xml = writer.getBuffer().toString().replaceAll("\n|\r", ""); return xml; |
String | documentToString(Document doc) Convert from Document to String 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(); |
String | documentToString(Document doc) Simple method to dump a Document object as a xml string. StringWriter writer = new StringWriter(); getDOCUMENT_TRANSFORMER().transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); |