Java XML Node to String GetOuterXml(Node node)

Here you can find the source of GetOuterXml(Node node)

Description

Gets the outerXML from a Node

License

Open Source License

Parameter

Parameter Description
node a parameter

Exception

Parameter Description
TransformerConfigurationException an exception
TransformerException an exception

Return

String - the outer XML

Declaration

public static String GetOuterXml(Node node)
        throws TransformerConfigurationException, TransformerException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

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.Node;

public class Main {
    /**//  w  ww.  j  a va2 s  . com
     * Gets the outerXML from a Node
     * @param node
     * @return String - the outer XML
     * @throws TransformerConfigurationException
     * @throws TransformerException
     */
    public static String GetOuterXml(Node node)
            throws TransformerConfigurationException, TransformerException {
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");
        StringWriter writer = new StringWriter();
        transformer
                .transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    }
}

Related

  1. getNodeAsInputStream(Node node)
  2. getNodeAsStream(Node node)
  3. getNodeSubtreeXMLString(final Node node)
  4. getNodeXml(Node node)
  5. getOuterXml(Node n)
  6. getStringFromDocument(Node doc)
  7. getStringFromNode(Node node)
  8. getStringFromNode(Node node)
  9. getStringFromXML(Node node)