Java XML Document to String documentToString(Document doc)

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

Description

document To String

License

Open Source License

Declaration

public static String documentToString(Document doc)
            throws ParserConfigurationException, TransformerConfigurationException, TransformerException 

Method Source Code

//package com.java2s;
/*************************************************************************
*                                                                        *
*  This file is part of the 20n/act project.                             *
*  20n/act enables DNA prediction for synthetic biology/bioengineering.  *
*  Copyright (C) 2017 20n Labs, Inc.                                     *
*                                                                        *
*  Please direct all queries to act@20n.com.                             *
*                                                                        *
*  This program is free software: you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation, either version 3 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 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 org.w3c.dom.Document;

import javax.xml.parsers.ParserConfigurationException;
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 java.io.StringWriter;

public class Main {
    private static final ThreadLocal<TransformerFactory> TRANSFORMER_FACTORY = new ThreadLocal<TransformerFactory>() {
        @Override// w  ww  .  ja  v a2 s .c  om
        protected TransformerFactory initialValue() {
            return TransformerFactory.newInstance();
        }
    };

    public static String documentToString(Document doc)
            throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
        StringWriter stringWriter = new StringWriter();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(stringWriter);
        Transformer transformer = getTransformerFactory().newTransformer();
        transformer.transform(source, result);
        return stringWriter.toString();
    }

    public static TransformerFactory getTransformerFactory() {
        return TRANSFORMER_FACTORY.get();
    }
}

Related

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