Java XML Node to String toXMLString(Node node)

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

Description

to XML String

License

LGPL

Declaration

public static String toXMLString(Node node) 

Method Source Code

//package com.java2s;
/*/*  w w w  .  j a  va  2s . co  m*/
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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 {
    private final static TransformerFactory tf = TransformerFactory.newInstance();

    public static String toXMLString(Node node) {
        if (node == null) {
            return "";
        }
        try {
            Transformer tran = tf.newTransformer();
            tran.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter swriter = new StringWriter();
            Source src = new DOMSource(node);
            Result res = new StreamResult(swriter);
            tran.transform(src, res);
            return swriter.getBuffer().toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}

Related

  1. toXml(Node node)
  2. toXml(Node node)
  3. toXML(Node node)
  4. toXml(Node node, boolean keepHeader)
  5. toXMLString(final Node value)
  6. toXMLString(Node node)
  7. toXMLString(Node node)
  8. toXMLString(Node node, boolean header)
  9. writeXml(Node n, OutputStream os)