Java XML Document to String toString(Node document)

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

Description

to String

License

Open Source License

Declaration

public static String toString(Node document) 

Method Source Code

//package com.java2s;
/*******************************************************************************
*
*  JOpac2 (C) 2002-2007 JOpac2 project/*w w w. ja  v  a  2 s  .c om*/
*
*     This file is part of JOpac2. http://jopac2.sourceforge.net
*
*  JOpac2 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 2 of the License, or
*  (at your option) any later version.
*
*  JOpac2 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 JOpac2; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*******************************************************************************/

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.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static String toString(Node document) {
        Transformer transformer;
        String output = null;
        if (document != null)
            try {
                transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");

                //         initialize StreamResult with File object to save to file
                StreamResult result = new StreamResult(new StringWriter());
                DOMSource source = new DOMSource(document);
                transformer.transform(source, result);

                output = result.getWriter().toString();

            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            } catch (TransformerFactoryConfigurationError e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            }

        return output;
    }
}

Related

  1. toString(Document document)
  2. toString(Document document)
  3. toString(final Document document)
  4. toString(final Document document)
  5. toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
  6. toStringFromDoc(Document document)
  7. toStructureString(Document document)
  8. toXML(Document document)
  9. toXML(Document dom)