Java Utililty Methods XML Document to String

List of utility methods to do XML Document to String

Description

The list of methods to do XML Document to String are organized into topic(s).

Method

Stringdocument2String(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);
...
Stringdocument2String(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();
...
Stringdocument2String(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();
Stringdocument2XmlString(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) {
...
StringdocumentToString(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();
StringdocumentToString(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);
...
StringdocumentToString(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;
StringdocumentToString(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;
StringdocumentToString(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();
StringdocumentToString(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();