Convert xml Node To String - Android XML

Android examples for XML:XML Node

Description

Convert xml Node To String

Demo Code


//package com.java2s;
import org.w3c.dom.Node;
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 java.io.StringWriter;

public class Main {
    private static TransformerFactory transFactory = TransformerFactory
            .newInstance();//from w w w .  j  a v a  2 s  . co m

    public static String xmlNodeToString(Node node, String encoding)
            throws TransformerException {
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty("encoding", encoding);
        StringWriter strWtr = new StringWriter();

        DOMSource source = new DOMSource();
        source.setNode(node);
        StreamResult result = new StreamResult(strWtr);
        transformer.transform(source, result);
        return strWtr.toString();

    }
}

Related Tutorials