Android XML Document to String Convert getXml(Document doc)

Here you can find the source of getXml(Document doc)

Description

get Xml

Declaration

public static String getXml(Document doc) throws TransformerException 

Method Source Code

//package com.java2s;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
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;
import org.w3c.dom.Document;

public class Main {
    public static String getXml(Document doc) throws TransformerException {
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(//from  w  w  w . j  a va2s . c o  m
                "{http://xml.apache.org/xslt}indent-amount",
                Integer.toString(2));

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc.getDocumentElement());

        trans.transform(source, result);
        return sw.toString();
    }
}

Related

  1. toXML(Document document)
  2. numResults(Document doc)
  3. documentToString(Node n)
  4. getErrorText(Document xml)
  5. xmlDocumentToString(Document document)