Java tutorial
//package com.java2s; /* * $Id: XPathUtils.java,v 1.4 2007/02/09 00:16:45 joshy Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.io.StringWriter; import java.util.Properties; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { /** * Exports this DOM as a String */ public static String toXML(Document dom) { return toXML(dom, null); } public static String toXML(Document dom, Properties outputProperties) { try { DOMSource source = new DOMSource(dom); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Transformer tx = TransformerFactory.newInstance().newTransformer(); if (outputProperties != null) { tx.setOutputProperties(outputProperties); } tx.transform(source, result); String s = writer.toString(); writer.close(); return s; } catch (Exception e) { e.printStackTrace(); return null; } } }