Java XML Node to String nodeToString(Node node)

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

Description

Serialized a node to a string.

License

Open Source License

Parameter

Parameter Description
node The node to be serialized.

Exception

Parameter Description
TransformerFactoryConfigurationError an exception
TransformerException an exception

Return

The serialized node.

Declaration

public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code


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

import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;

public class Main {
    /** /* ww w .j  ava 2  s  . co m*/
     * Serialized a node to a string. XML declarations will be omitted. 
     * 
     * @param node The node to be serialized.
     * 
     * @return The serialized node.
     * 
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerException
     */
    public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");
        StringWriter sw = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(sw));
        return sw.toString();
    }
}

Related

  1. nodeToString(Node node)
  2. nodeToString(Node node)
  3. nodeToString(Node node)
  4. nodeToString(Node node)
  5. nodeToString(Node node)
  6. nodeToString(Node node)
  7. nodeToString(Node node)
  8. nodeToString(Node node)
  9. nodeToString(Node node)