Java XML Node to String xml(Node node)

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

Description

xml

License

Open Source License

Declaration

public static String xml(Node node) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Node;

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;
import java.io.Writer;

public class Main {
    public static String xml(Node node) {
        TransformerFactory factory = TransformerFactory.newInstance();
        return xml(factory, node);
    }//from ww  w  . j av a  2 s.  c  om

    public static String xml(TransformerFactory transformerFactory, Node node) {
        Writer writer = new StringWriter();
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(writer);
        try {
            transformerFactory.newTransformer().transform(source, result);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
        return writer.toString().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    }
}

Related

  1. toXMLString(Node node)
  2. toXMLString(Node node)
  3. toXMLString(Node node)
  4. toXMLString(Node node, boolean header)
  5. writeXml(Node n, OutputStream os)
  6. XML2String(Node node)
  7. xmlToBytes(final Node body)
  8. XmlToSource(Node node)
  9. xmlToStream(Node n, OutputStream os)