Java XML Document to String documentToString(Document doc)

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

Description

Convert from Document to String

License

Open Source License

Parameter

Parameter Description
doc the Document to convert

Return

the resulting String

Declaration

private static String documentToString(Document doc)
        throws TransformerConfigurationException, TransformerException 

Method Source Code

//package com.java2s;
/*//from   w ww. j a v  a  2  s. co  m
 *  This file is part of "Tweety", a collection of Java libraries for
 *  logical aspects of artificial intelligence and knowledge representation.
 *
 *  Tweety is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import java.io.StringWriter;

import org.w3c.dom.Document;

public class Main {
    /**
     * Convert from {@code Document} to {@code String}
     * @param doc the {@code Document} to convert
     * @return the resulting {@code String}
     */
    private static String documentToString(Document doc)
            throws TransformerConfigurationException, TransformerException {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    }
}

Related

  1. document2XmlString(Document xmldoc)
  2. documentToString(Document d)
  3. documentToString(Document d)
  4. documentToString(Document doc)
  5. documentToString(Document doc)
  6. documentToString(Document doc)
  7. documentToString(Document doc)
  8. documentToString(Document doc)
  9. documentToString(Document doc)