Here you can find the source of toStringNoIndent(Element aElement)
aElement
to a String
for printing without pretty printing.
Parameter | Description |
---|---|
aElement | <code>Element</code> to print |
String
representation of Element
if successful; "ERROR" String
otherwise.
public static String toStringNoIndent(Element aElement)
//package com.java2s; /*********************************************************** Copyright (C) 2004 VeriSign, Inc./*from www . j av a 2 s. co m*/ 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 http://www.verisign.com/nds/naming/namestore/techdocs.html ***********************************************************/ import org.w3c.dom.*; 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 java.io.ByteArrayOutputStream; public class Main { /** * Converts an <code>aElement</code> to a <code>String</code> for printing * without pretty printing. * * @param aElement * <code>Element</code> to print * @return <code>String</code> representation of <code>Element</code> if * successful; "ERROR" <code>String</code> otherwise. */ public static String toStringNoIndent(Element aElement) { String ret = "ERROR"; ByteArrayOutputStream theBuffer = new ByteArrayOutputStream(); // Serialize DOM Document to stream try { TransformerFactory transFac = TransformerFactory.newInstance(); Transformer trans = transFac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.transform(new DOMSource(aElement), new StreamResult( theBuffer)); theBuffer.close(); ret = new String(theBuffer.toByteArray()); } catch (Exception ex) { ex.printStackTrace(); } return ret; } }