Java XML Document to String XMLToString(Document XMLDocument)

Here you can find the source of XMLToString(Document XMLDocument)

Description

Converts DOM document to a string

License

Open Source License

Parameter

Parameter Description
XMLDocument the document you want to convert

Return

String representation of you XML document

Declaration

public static String XMLToString(Document XMLDocument) 

Method Source Code

//package com.java2s;
/**/*from  w  w w .ja  va2s  .c  o  m*/
(C) Copyright 1998-2007 Hewlett-Packard Development Company, LP
    
This library 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 2.1 of the License, or (at your option) any later version.
    
This library 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 this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
For more information: www.smartfrog.org
*/

import org.w3c.dom.Document;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    /**
     * Converts DOM document to a string
     *
     * @param XMLDocument the document you want to convert
     * @return  String representation of you XML document
     */
    public static String XMLToString(Document XMLDocument) {
        String xmlString = null;
        try {
            // Convert DOM to XML string
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            Transformer trans = TransformerFactory.newInstance().newTransformer();
            //trans.setOutputProperty(OutputKeys.INDENT, "yes");
            trans.transform(new DOMSource(XMLDocument), result);
            xmlString = sw.toString();
            sw.close();
        } catch (TransformerConfigurationException tcex) {

        } catch (TransformerException tex) {

        } catch (IOException iex) {

        }
        return xmlString;
    }
}

Related

  1. xmlToString(Document doc)
  2. xmlToString(Document doc)
  3. xmlToString(Document document)
  4. xmlToString(Document xml)
  5. xmlToString(Document xml)
  6. xmltoString(final Document document)
  7. xmlToStringBuffer(Document doc, int identAmount)