Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; 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; import org.w3c.dom.NodeList; public class Main { /** * Get standard xml string of node * * @param node * @return */ public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); } /** * Get standard xml string of nodes * * @param nl * @return */ public static String nodeToString(NodeList nl) { StringBuilder s = new StringBuilder(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); s.append(nodeToString(n)); } return s.toString(); } }