Java XML Node to String asString(Node node)

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

Description

Creates a String out of a Node

License

Apache License

Parameter

Parameter Description
node never <code>null</code>

Exception

Parameter Description
Exception an exception

Return

the node as string, never null

Declaration

public static String asString(Node node) throws Exception 

Method Source Code

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

import java.io.StringWriter;

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 {
    /**//from w w  w.ja va 2s  . c  o m
     * Creates a {@link String} out of a {@link Node}
     * 
     * @param node
     *            never <code>null</code>
     * @return the node as string, never <code>null</code>
     * @throws Exception
     */
    public static String asString(Node node) throws Exception {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    }
}

Related

  1. asString(Element elt)
  2. asString(Node node)
  3. asString(Node node)
  4. asString(XMLStreamReader xmlr)
  5. asXML(Node node)
  6. asXmlString(Node node)
  7. convertToString(Node node)