Java XML Node to String nodeToString(final Node node)

Here you can find the source of nodeToString(final Node node)

Description

Convert XML Node to String

License

Apache License

Parameter

Parameter Description
node the node to convert

Exception

Parameter Description
TransformerException an exception

Return

the String equivalent of the node

Declaration

public static String nodeToString(final Node node) throws TransformerException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
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 {
    /**/* w  w  w .ja  v  a2s.  c  o m*/
     * Convert XML Node to String
     *
     * @param node
     *            the node to convert
     * @return the String equivalent of the node
     * @throws TransformerException
     */
    public static String nodeToString(final Node node) throws TransformerException {
        final Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        final Writer out = new StringWriter();
        tf.transform(new DOMSource(node), new StreamResult(out));
        return out.toString();
    }
}

Related

  1. getXMLString(Node node, boolean omitXmlDeclaration)
  2. getXMLStringFragmentFromNode(Node node)
  3. nodeToStr(final Node node)
  4. nodeToString(final Node node)
  5. nodeToString(final Node node)
  6. nodeToString(final Node node)
  7. nodeToString(Node doc)
  8. nodeToString(Node n)
  9. nodeToString(Node n)