Java tutorial
//package com.java2s; /** (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; } }