Java XML Node to String getXMLString(Node node, boolean omitXmlDeclaration)

Here you can find the source of getXMLString(Node node, boolean omitXmlDeclaration)

Description

Returns the XML string representation of a Node.

License

Open Source License

Parameter

Parameter Description
node Node to convert into XML string.
omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string.

Exception

Parameter Description
TransformerConfigurationException an exception
TransformerException an exception

Return

The XML string representation of the input node.

Declaration

public static String getXMLString(Node node, boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerException 

Method Source Code

//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));
    }
}

Related

  1. getText(final Node node)
  2. getXml(Node node)
  3. getXmlAsString(Node node)
  4. getXMLString(Node doc)
  5. getXmlString(Node n)
  6. getXMLStringFragmentFromNode(Node node)
  7. nodeToStr(final Node node)
  8. nodeToString(final Node node)
  9. nodeToString(final Node node)