Java XML Document to String document2XmlString(Document xmldoc)

Here you can find the source of document2XmlString(Document xmldoc)

Description

document Xml String

License

Apache License

Declaration

public static String document2XmlString(Document xmldoc) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    public static String document2XmlString(Document xmldoc) {
        try {/*  w  w w.j  a  v  a2s .co m*/
            Source src = new DOMSource(xmldoc);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(); // identity transformer
            transformer.transform(src, new StreamResult(bout));
            return bout.toString("UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. document2String(Document doc)
  2. document2String(Document doc, boolean prettyPrint)
  3. document2String(Node document)
  4. documentToString(Document d)
  5. documentToString(Document d)
  6. documentToString(Document doc)
  7. documentToString(Document doc)