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

StringtoString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
to String
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
final StringWriter stringWriter = new StringWriter();
final StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(new DOMSource(document), streamResult);
...
StringtoString(Node document)
to String
Transformer transformer;
String output = null;
if (document != null)
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(document);
...
StringtoStringFromDoc(Document document)
to String From Doc
String result = null;
if (document != null) {
    StringWriter strWtr = new StringWriter();
    StreamResult strResult = new StreamResult(strWtr);
    TransformerFactory tfac = TransformerFactory.newInstance();
    try {
        javax.xml.transform.Transformer t = tfac.newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
...
StringtoStructureString(Document document)
to Structure String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
NodeList childNodes = document.getChildNodes();
printNodes(pw, childNodes, 0);
return sw.toString();
StringtoXML(Document document)
Serializes a DOM Document to XML
DOMImplementationLS domLS = (DOMImplementationLS) implementation;
LSSerializer serializer = domLS.createLSSerializer();
String s = serializer.writeToString(document);
return s;
StringtoXML(Document dom)
Exports this DOM as a String
return toXML(dom, null);
StringtoXml(Document domDoc)
to Xml
DOMImplementation domImplementation = domDoc.getImplementation();
if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
            "3.0");
    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
    if (domConfiguration.canSetParameter("xml-declaration", Boolean.TRUE))
        lsSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);
...
StringtoXmlString(Document doc)
to Xml 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);
writer.flush();
return writer.toString();
...
StringtoXmlString(Document doc)
to Xml String
String xmlString = null;
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
...
StringtoXMLString(Document doc, boolean includeXMLDecl, boolean indent)
to XML String
String xmlString = null;
try {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, includeXMLDecl ? "yes" : "no");
    trans.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
...