Java XML Node to String xmlToString(Node node)

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

Description

xml To String

License

Open Source License

Declaration

public static String xmlToString(Node node) throws TransformerException 

Method Source Code

//package com.java2s;
/** Escaping code for XML borrowed from org.json.XML
 * Full license and code available https://github.com/douglascrockford/JSON-java/blob/master/XML.java
 * @param string The string to be encoded
 * @return An encoded copy of the string
 **//*  w w  w .j  ava 2  s  .  c o m*/

import java.io.StringWriter;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.Node;

public class Main {
    public static String xmlToString(Node node) throws TransformerException {
        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();
    }
}

Related

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