Here you can find the source of getStringFromNode(Node node)
Parameter | Description |
---|---|
node | The node to convert. |
public static String getStringFromNode(Node node)
//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) { } } } }