Java XML Document to String toString(Document document)

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

Description

to String

License

Open Source License

Declaration

public static String toString(Document document) 

Method Source Code


//package com.java2s;
/*/*  w ww  . j a v  a 2  s.co m*/
 * $Id$
 *
 * Copyright (c) 2002, 2004 Takao Nakaguchi.
 * 
 * This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services.
 * Copyright (C) 2005-2008 NICT Language Grid Project.
 *
 * This program 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 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. 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 program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.UnsupportedEncodingException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    private static Transformer defaultTextTransformer;

    /**
     * 
     * 
     */
    public static String toString(Document document) {
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            defaultTextTransformer.transform(new DOMSource(document), new StreamResult(out));
            return new String(out.toByteArray(), "UTF-8");
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
                out = null;
            }
        }
    }
}

Related

  1. toString(Document doc)
  2. toString(Document doc)
  3. toString(Document doc)
  4. toString(Document doc)
  5. toString(Document doc, String encoding, boolean indent)
  6. toString(Document document)
  7. toString(Document document)
  8. toString(Document document)
  9. toString(Document document)