Here you can find the source of getXMLString(Node node, boolean omitXmlDeclaration)
Parameter | Description |
---|---|
node | Node to convert into XML string. |
omitXmlDeclaration | <tt>true</tt> to remove the XML declaration from the string. |
Parameter | Description |
---|---|
TransformerConfigurationException | an exception |
TransformerException | an exception |
public static String getXMLString(Node node, boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerException
//package com.java2s; /*/*w ww. j av a 2 s .c o m*/ * The following methods come from a library written by Tom Fennelly. * Here was the header of the licence. */ import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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.*; public class Main { /** * Returns the XML string representation of a Node. * @param node Node to convert into XML string. * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. * @return The XML string representation of the input node. * @throws TransformerConfigurationException * @throws TransformerException */ public static String getXMLString(Node node, boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerException { StringWriter writer = new StringWriter(); DOMSource domSource = new DOMSource(node); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); serializer.transform(domSource, result); return (writer.toString()); } /** * Returns the XML string representation of a Node. The XML declaration will not be omitted. * @param node Node to convert into XML string. * @return The XML string representation of the input node. * @throws TransformerConfigurationException * @throws TransformerException */ public static String getXMLString(Node node) throws TransformerConfigurationException, TransformerException { return (getXMLString(node, false)); } }