Java XML Node to String toString(final Node node)

Here you can find the source of toString(final Node node)

Description

Returns the content of the given node as string

License

Open Source License

Declaration

public static String toString(final Node node) 

Method Source Code

//package com.java2s;
/*//from w  w w .  j  av  a 2 s . c  om
This file is part of Cyclos (www.cyclos.org).
A project of the Social Trade Organisation (www.socialtrade.org).
    
Cyclos is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
Cyclos is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Cyclos; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
 */

import java.io.StringWriter;

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.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    /**
     * Returns the content of the given node as string
     */
    public static String toString(final Node node) {
        final StringWriter out = new StringWriter();
        write(node, new StreamResult(out));
        return out.toString();
    }

    private static void write(final Node node, final Result result) {
        try {
            // Prepare the DOM document for writing
            final Source source = new DOMSource(node);

            // Write the DOM document to the file
            final Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
        } catch (final TransformerConfigurationException e) {
        } catch (final TransformerException e) {
        }

    }
}

Related

  1. nodeToXmlString(Node node)
  2. stringifyNode(Node node)
  3. toHtml(StringBuffer html, Node node)
  4. toString(final Node node)
  5. toString(final Node node)
  6. toString(final Node xml)
  7. toString(final short nodeType)
  8. toString(Node element)
  9. toString(Node n)