get XML String from Node using Transformer - Java XML

Java examples for XML:DOM Node Value

Description

get XML String from Node using Transformer

Demo Code


//package com.java2s;
import java.io.StringWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.Node;

public class Main {

    public static String getXMLString(Node doc)
            throws TransformerException, TransformerConfigurationException {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();

        StringWriter sw = new StringWriter();

        Source src = new DOMSource(doc);
        Result dest = new StreamResult(sw);

        aTransformer.transform(src, dest);

        return sw.toString();
    }//from w  w w.j a v a 2 s . c  om
}

Related Tutorials