Java XML Element to String toStringNoIndent(Element aElement)

Here you can find the source of toStringNoIndent(Element aElement)

Description

Converts an aElement to a String for printing without pretty printing.

License

Open Source License

Parameter

Parameter Description
aElement <code>Element</code> to print

Return

String representation of Element if successful; "ERROR" String otherwise.

Declaration

public static String toStringNoIndent(Element aElement) 

Method Source Code

//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;
    }
}

Related

  1. getXMLStringFromNode(Element node)
  2. toString(Element e)
  3. toString(Element element)
  4. toString(Element node)
  5. toString(Element xml)