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

StringgetString(Document document)
get String
StreamResult streamResult;
try {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    streamResult = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(document);
...
StringgetStringFromDoc(org.w3c.dom.Document doc)
Prints a DOM document to XML using DOM Load and Save's LSSerializer.
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
return lsSerializer.writeToString(doc);
StringgetStringFromDocument(Document doc)
get String From Document
try {
    doc.normalize();
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
...
StringgetStringFromDocument(Document doc)
Convert Document element to a String object
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(domSource, result);
return writer.toString().substring(writer.toString().indexOf('>') + 1);
StringgetStringFromDOM(Document doc)
Returns an XML string given a DOM document.
TransformerFactory transFac = TransformerFactory.newInstance();
Transformer transformer = transFac.newTransformer();
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bout);
transformer.transform(source, result);
String xml = new String(bout.toByteArray());
bout.close();
...
StringgetStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)
Converts a DOM document to an xml String.
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
if (null != xslt) {
    DOMSource dxsltsource = new DOMSource(xslt);
    transformer = tf.newTransformer(dxsltsource);
...
StringgetStringFromXMLDocument(Document doc)
Serializes an XML document to a string
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
return lsSerializer.writeToString(doc);
StringgetStringFromXPath(Document doc, XPath xpath, String expression)
Gets a string from an xml.
synchronized (xpath) {
    try {
        return xpath.evaluate(expression, doc);
    } catch (Exception e) {
        System.err.println("Error evaluating xpath expression: " + expression);
        e.printStackTrace();
    return "";
...
ListgetStringListFromXPath(Document doc, XPath xpath, String rootNodeExpression, String listExpression)
Gets a list of strings from an xml.
synchronized (xpath) {
    try {
        return getStringListFromXPath((Node) xpath.evaluate(rootNodeExpression, doc, XPathConstants.NODE),
                xpath, listExpression);
    } catch (Exception e) {
        System.err.println("Error evaluating xpath expression: " + rootNodeExpression);
        e.printStackTrace();
    return new Vector<String>();
StringgetStringRepresentation(DocumentFragment df)
get String Representation
StringBuilder sb = new StringBuilder();
NodeList nl = df.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node n = nl.item(i);
    sb.append(nodeToString(n));
return sb.toString();