Java XML Node to String XML2String(Node node)

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

Description

XML String

License

Open Source License

Declaration

public static String XML2String(Node node) 

Method Source Code

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

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static String XML2String(Node node) {
        try {/*from   w w w. j a v a2  s .c o  m*/
            StringWriter writer = new StringWriter();
            Transformer trans = TransformerFactory.newInstance().newTransformer();
            trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            Source source = new DOMSource(node);
            Result result = new StreamResult(writer);
            trans.transform(source, result);
            return writer.toString();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. toXMLString(Node node)
  2. toXMLString(Node node)
  3. toXMLString(Node node, boolean header)
  4. writeXml(Node n, OutputStream os)
  5. xml(Node node)
  6. xmlToBytes(final Node body)
  7. XmlToSource(Node node)
  8. xmlToStream(Node n, OutputStream os)
  9. xmlToStreamE(Node n, OutputStream os)