Here you can find the source of toXML(Node node)
Parameter | Description |
---|---|
node | Node to show as String |
public static final String toXML(Node node) throws TransformerFactoryConfigurationError, TransformerException
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import java.io.Writer; import javax.xml.transform.OutputKeys; 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 { /**//from w w w .j a v a2 s.c om * Takes a {@link Node} object and converts it to a {@link String} representing the valid XML structure. * * @param node * {@link Node} to show as {@link String} * @return The {@link Node} as {@link String} */ public static final String toXML(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); } }