Java XML String Transform elementToXMLString(Object aElement)

Here you can find the source of elementToXMLString(Object aElement)

Description

element To XML String

License

LGPL

Declaration

public static String elementToXMLString(Object aElement) throws TransformerException 

Method Source Code

//package com.java2s;
/*/*from www.ja  v a2s. com*/
 * IST-FP6-034763 QualiPSo: QualiPSo is a unique alliance
 * of European, Brazilian and Chinese ICT industry players,
 * SMEs, governments and academics to help industries and
 * governments fuel innovation and competitiveness with Open
 * Source software. To meet that goal, the QualiPSo consortium
 * intends to define and implement the technologies, processes
 * and policies to facilitate the development and use of Open
 * Source software components, with the same level of trust
 * traditionally offered by proprietary software. QualiPSo is
 * partially funded by the European Commission under EU?s sixth
 * framework program (FP6), as part of the Information Society
 * Technologies (IST) initiative. 
 *
 * This program has been created as part of the QualiPSo work
 * package on "Semantic Interoperability". The basic idea of this work    
 * package is to demonstrate how semantic technologies can be used to 
 * cope with the diversity and heterogeneity of software and services 
 * in the OSS domain.
 *
 * You can redistribute this program and/or modify it under the terms 
 * of the GNU Lesser General Public License version 3 (LGPL v3.0).
 *
 * This program 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.  
 *
 * You should have received a copy of the LGPL
 * along with this program; if not, please have a look at
 * http://www.gnu.org/licenses/lgpl.html 
 * to obtain the full license text.
 *
 * Author of this program: 
 * Fraunhofer Institute FOKUS, http://www.fokus.fraunhofer.de
 *
 *
*/

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    /**
     * Property for setting indent levels.
     */
    protected static final String INDENT_AMOUNT_XALAN = "{http://xml.apache.org/xalan}indent-amount";

    public static String elementToXMLString(Object aElement) throws TransformerException {
        DOMSource source = new DOMSource((Node) aElement);
        StringWriter stringWriter = new StringWriter();
        StreamResult result = new StreamResult(stringWriter);

        TransformerFactory tfactory = TransformerFactory.newInstance();
        if (tfactory.getFeature(DOMSource.FEATURE) && tfactory.getFeature(StreamResult.FEATURE)) {
            Transformer transformer = tfactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(INDENT_AMOUNT_XALAN, "3");

            transformer.transform(source, result);
        }

        return stringWriter.toString();
    }
}

Related

  1. createString(Element element)
  2. createXml(ArrayList nameList, ArrayList stringList, ArrayList stringArrayNameList, HashMap> stringArrayContentMap)
  3. createXmlError(String message, String details)
  4. createXmlEventReaderOnXmlString(String xml, String docBaseUri)
  5. decodeHex(String hex)
  6. encode(final String value)
  7. encodeStringIntoMemento(String str)
  8. escapeBackslashes(String value)
  9. extractAndNormalizeEmbedPictures(String xmlFile, String odtFile, String parentDir, String imgBaseDir)