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

@Deprecated
public static String toString(Document document)
        throws TransformerConfigurationException, TransformerException 

Method Source Code


//package com.java2s;
/*/*from  w  ww  . jav a2 s . co  m*/
 * ao-lang - Minimal Java library with no external dependencies shared by many other projects.
 * Copyright (C) 2014, 2016, 2017  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of ao-lang.
 *
 * ao-lang 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 3 of the License, or
 * (at your option) any later version.
 *
 * ao-lang 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 ao-lang.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
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 {
    /**
     * @deprecated  Use {@link #toString(org.w3c.dom.Node)} instead.
     */
    @Deprecated
    public static String toString(Document document)
            throws TransformerConfigurationException, TransformerException {
        return toString((Node) document);
    }

    public static String toString(Node node) throws TransformerConfigurationException, TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 4);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        try {
            transformer.transform(new DOMSource(node), new StreamResult(writer));
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                // Java 1.7: direct constructor
                AssertionError ae = new AssertionError("IOException should never be thrown from StringWriter");
                ae.initCause(e);
                throw ae;
            }
        }
        return writer.toString();
    }
}

Related

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