Java XML Document to String documentToString(final Node node)

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

Description

Converts the given document to string format by passing it through a transformer.

License

Open Source License

Parameter

Parameter Description
node the node to convert to a java string.

Exception

Parameter Description
TransformerException if a transformation error occurs

Return

the XML string result

Declaration

public static String documentToString(final Node node) throws TransformerException 

Method Source Code

//package com.java2s;
/**/*w  w w .j av a  2 s  .  c o m*/
 * This file is part of the au-xml-util package
 *
 * Copyright Trenton D. Adams <trenton daught d daught adams at gmail daught ca>
 * 
 * au-xml-util is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * au-xml-util 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with au-xml-util.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * See the COPYING file for more information.
 */

import org.w3c.dom.Node;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.*;

public class Main {
    static final int BUFFER_CAPACITY = 50000;

    /**
     * Converts the given document to string format by passing it through a
     * transformer.
     *
     * @param node the node to convert to a java string.
     *
     * @return the XML string result
     *
     * @throws TransformerException if a transformation error occurs
     */
    public static String documentToString(final Node node) throws TransformerException {
        final TransformerFactory transformerFactory;
        final Transformer transformer;
        final DOMSource source;
        final StreamResult result;
        final StringWriter writer;

        writer = new StringWriter(BUFFER_CAPACITY);
        transformerFactory = TransformerFactory.newInstance();
        transformer = transformerFactory.newTransformer();
        source = new DOMSource(node);
        result = new StreamResult(writer);
        transformer.transform(source, result);

        return writer.toString();
    }
}

Related

  1. documentToString(Document document)
  2. documentToString(Document document, boolean pretty)
  3. documentToString(Document document, boolean standalone)
  4. documentToString(Document document, Transformer documentTransformer)
  5. DocumentToString(Document dom)
  6. documentToString(Node document)
  7. dom2String2(Document document)
  8. DOMaXML(Document doc, String nome)
  9. domToString(Document document)