Java XML Node to String xmlToString(Node node)

Here you can find the source of xmlToString(Node node)

Description

Convert XML Document to a String.

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

public static String xmlToString(Node node) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

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 {
    /**//from   w  w  w .j  a v a 2  s. c  o  m
     * Convert XML Document to a String.
     * 
     * @param node
     * @return
     */
    public static String xmlToString(Node node) {
        try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. xmlToStream(Node n, OutputStream os)
  2. xmlToStreamE(Node n, OutputStream os)
  3. xmlToString(Node doc)
  4. xmlToString(Node doc)
  5. xmlToString(Node node)
  6. xmlToString(Node node)
  7. xmlToString(Node node)