/* * This file is part of INDI for Java. * * INDI for Java 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. * * INDI for Java 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 INDI for Java. If not, see * <http://www.gnu.org/licenses/>. */ package laazotea.indi; import java.io.StringWriter; import javax.xml.transform.OutputKeys; 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.Element; /** * A class to transforms XML Elements into Strings. * * @author S. Alonso (Zerjillo) [zerjioi at ugr.es] * @version 1.34, November 8, 2013 */ public class XMLToString { /** * Transforms a XML Element into a String. * * @param xml The XML Element * @return A String representing the XML Element */ public static String transform(Element xml) { try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(xml), new StreamResult(buffer)); String str = buffer.toString(); return str; } catch (Exception e) { } return ""; } }