Java XML Document to String toString(Document doc)

Here you can find the source of toString(Document doc)

Description

This method is used to convert a Document object into an XML String.

License

Open Source License

Parameter

Parameter Description
doc a parameter

Exception

Parameter Description
TransformerConfigurationException an exception
TransformerException an exception

Declaration

public static String toString(Document doc) throws TransformerConfigurationException, TransformerException 

Method Source Code

//package com.java2s;
/** Exhibit A - UIRF Open-source Based Public Software License.
* 
* The contents of this file are subject to the UIRF Open-source Based
* Public Software License(the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* openelis.uhl.uiowa.edu/*  w  w w.j  a va  2s.c o m*/
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
* 
* The Original Code is OpenELIS code.
* 
* The Initial Developer of the Original Code is The University of Iowa.
* Portions created by The University of Iowa are Copyright 2006-2008. All
* Rights Reserved.
* 
* Contributor(s): ______________________________________.
* 
* Alternatively, the contents of this file marked
* "Separately-Licensed" may be used under the terms of a UIRF Software
* license ("UIRF Software License"), in which case the provisions of a
* UIRF Software License are applicable instead of those above. 
*/

import java.io.ByteArrayOutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.Document;

import org.w3c.dom.Node;

public class Main {
    /**
     * This method is used to convert a Document object into an XML String.
     * 
     * @param doc
     * @return
     * @throws TransformerConfigurationException
     * @throws TransformerException
     */
    public static String toString(Document doc) throws TransformerConfigurationException, TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMSource source = new DOMSource(doc);
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(output);
            transformer.transform(source, result);

            return new String(output.toByteArray(), "UTF-8");
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * This method is used to convert a Node object into an XML String.
     * 
     * @param node
     * @return
     * @throws TransformerConfigurationException
     * @throws TransformerException
     */
    public static String toString(Node node) throws TransformerConfigurationException, TransformerException {
        ByteArrayOutputStream output;
        DOMSource source;
        StreamResult result;
        Transformer transformer;

        transformer = TransformerFactory.newInstance().newTransformer();
        source = new DOMSource(node);
        try {
            output = new ByteArrayOutputStream();
            result = new StreamResult(output);
            transformer.transform(source, result);
            return new String(output.toByteArray(), "UTF-8");
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getXML(Document pDocument)
  2. toByteArray(final Document document)
  3. toByteArray(final Document document)
  4. toStream(Document document, OutputStream stream)
  5. toString(Document d)
  6. toString(Document doc)
  7. toString(Document doc)
  8. toString(Document doc)
  9. toString(Document doc)