Java XML Node to String xmlToString(Node node)

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

Description

convert an xml document to a string

License

Open Source License

Parameter

Parameter Description
node document node

Return

a string representation of the xml document

Declaration

public static String xmlToString(Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Crafter Studio Web-content authoring solution
 *     Copyright (C) 2007-2013 Crafter Software Corporation.
 * /* www  .ja v  a  2 s  .c  o m*/
 *     This program 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 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class Main {
    /**
     * convert an xml document to a string
     * 
     * @param node
     *            document node
     * @return a string representation of the xml document
     */
    public static String xmlToString(Node node) {

        return xmlToString(node, "UTF-8");
    }

    /**
     * convert an xml document to a string
     * 
     * @param node
     *            document node
     * @return a string representation of the xml document
     */
    public static String xmlToString(Node node, String encoding) {

        String retXmlAsString = "";

        try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);

            transformer.transform(source, result);
            retXmlAsString = stringWriter.toString();

            // for some reason encoding is not handling entity references - need
            // to look in to the further
            retXmlAsString = retXmlAsString.replace("&nbsp;", "&#160;");

        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        return retXmlAsString;
    }
}

Related

  1. xmlToStreamE(Node n, OutputStream os)
  2. xmlToString(Node doc)
  3. xmlToString(Node doc)
  4. xmlToString(Node node)
  5. xmlToString(Node node)
  6. xmlToString(Node node)