Java XML Node to String getOuterXml(Node n)

Here you can find the source of getOuterXml(Node n)

Description

get Outer Xml

License

LGPL

Declaration

public static String getOuterXml(Node n) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {
    public static String getOuterXml(Node n) {

        //For debug only...
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {//from   w  w  w  .  ja v  a 2 s .c  o  m
            transformer = transFactory.newTransformer();
        } catch (TransformerConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return "";
        }
        try {
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(new DOMSource(n), new StreamResult(buffer));
            return buffer.toString();

        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }

    }
}

Related

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