Java XML Element to String toString(Element e)

Here you can find the source of toString(Element e)

Description

to String

License

Apache License

Declaration

public static String toString(Element e) 

Method Source Code

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

import org.w3c.dom.Element;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import java.io.StringWriter;

public class Main {
    public static String toString(Element e) {
        try {// w  w w .j  a  v  a2s  .c om
            TransformerFactory tfactory = TransformerFactory.newInstance();
            Transformer xform = tfactory.newTransformer();
            Source src = new DOMSource(e);
            java.io.StringWriter writer = new StringWriter();
            Result result = new javax.xml.transform.stream.StreamResult(writer);
            xform.transform(src, result);
            return writer.toString();
        } catch (Exception ex) {
            return "Unable to convert to string: " + ex.toString();
        }
    }
}

Related

  1. getStringValueByTagName(Element element, String tagName)
  2. getStringValueOfElement(Element elem)
  3. getStringValues(Element from, String elementName)
  4. getXmlString(Element node)
  5. getXMLStringFromNode(Element node)
  6. toString(Element element)
  7. toString(Element node)
  8. toString(Element xml)
  9. toStringNoIndent(Element aElement)