Java XML Document to String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)

Here you can find the source of getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)

Description

Converts a DOM document to an xml String.

License

Apache License

Parameter

Parameter Description
doc DOM document

Exception

Parameter Description
TransformerException an exception

Return

xml String.

Declaration

public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)
        throws TransformerException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Main {
    /**// www  . ja v a2  s.c  om
     * Converts a DOM document to an xml String.
     *
     * @param doc DOM document
     * @return xml String.
     * @throws TransformerException
     */
    public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)
            throws TransformerException {
        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);
        } else {
            transformer = tf.newTransformer();
        }
        transformer.transform(domSource, result);
        return writer.toString();
    }
}

Related

  1. getString(Document document)
  2. getStringFromDoc(org.w3c.dom.Document doc)
  3. getStringFromDocument(Document doc)
  4. getStringFromDocument(Document doc)
  5. getStringFromDOM(Document doc)
  6. getStringFromXMLDocument(Document doc)
  7. getStringFromXPath(Document doc, XPath xpath, String expression)
  8. getStringListFromXPath(Document doc, XPath xpath, String rootNodeExpression, String listExpression)
  9. getStringRepresentation(DocumentFragment df)