Java XML Node to String getStringFromNode(Node node)

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

Description

Converts a node to a string.

License

LGPL

Parameter

Parameter Description
node The node to convert.

Return

A string representation of the node.

Declaration

public static String getStringFromNode(Node node) 

Method Source Code


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

import org.w3c.dom.*;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.*;

public class Main {
    /**/* w  w  w  .jav  a  2s.co  m*/
     * Converts a node to a string.
     * @param node The node to convert.
     * @return A string representation of the node.
     */
    public static String getStringFromNode(Node node) {
        DOMSource domSource = new DOMSource(node);
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(baos);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.transform(domSource, result);
            baos.flush();
            return new String(baos.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException("Failed to stream node to string", e);
        } finally {
            try {
                baos.close();
            } catch (Exception e) {
            }
        }

    }
}

Related

  1. getNodeSubtreeXMLString(final Node node)
  2. getNodeXml(Node node)
  3. getOuterXml(Node n)
  4. GetOuterXml(Node node)
  5. getStringFromDocument(Node doc)
  6. getStringFromNode(Node node)
  7. getStringFromXML(Node node)
  8. getText(final Node node)
  9. getXml(Node node)