Java XML Document Transform transformXMLtoString(Document xmldoc)

Here you can find the source of transformXMLtoString(Document xmldoc)

Description

transform a XML file to String

License

Open Source License

Parameter

Parameter Description
Document The XML document

Exception

Parameter Description
TransformerException if an error occurs.

Return

The content of the XML in String

Declaration

public static String transformXMLtoString(Document xmldoc) throws TransformerException 

Method Source Code

//package com.java2s;
/*/*from   w  ww  . j  av a 2s.  co m*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
 *
 * The contents of this file are subject to the terms of the Common 
 * Development and Distribution License ("CDDL")(the "License"). You 
 * may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://open-dm-mi.dev.java.net/cddl.html
 * or open-dm-mi/bootstrap/legal/license.txt. See the License for the 
 * specific language governing permissions and limitations under the  
 * License.  
 *
 * When distributing the Covered Code, include this CDDL Header Notice 
 * in each file and include the License file at
 * open-dm-mi/bootstrap/legal/license.txt.
 * If applicable, add the following below this CDDL Header, with the 
 * fields enclosed by brackets [] replaced by your own identifying 
 * information: "Portions Copyrighted [year] [name of copyright owner]"
 */

import java.io.ByteArrayOutputStream;

import java.io.OutputStreamWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
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.DOMConfiguration;
import org.w3c.dom.Document;

public class Main {
    /**
     * transform a XML file to String
     *
     * @param Document The XML document
     * @return The content of the XML in String
     * @throws TransformerException if an error occurs.
     */
    public static String transformXMLtoString(Document xmldoc) throws TransformerException {
        DOMConfiguration domConfig = xmldoc.getDomConfig();
        //domConfig.setParameter("format-pretty-print", "true");
        DOMSource domSource = new DOMSource(xmldoc);
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 4);
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.VERSION, "1.0");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Result result = new StreamResult(new OutputStreamWriter(out));
        serializer.transform(domSource, result);
        return out.toString();
    }
}

Related

  1. transformDocument(Document xmlDocument, String xsltFilename)
  2. transformDocumentAsString(Document xmlDocument, Map parameters, String xsltFilename)
  3. transformDomDocument(Transformer transformer, Node node, OutputStream os)
  4. transformXmlToString(Document importPackageDocument)
  5. transformXmlToString(Document payload)
  6. xslt(InputStream stylesheet, Document input)
  7. xslTransform(Document xmlDoc, Document xslDoc)