Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*!
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 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.
 *
 * Copyright (c) 2002-2013 Pentaho Corporation..  All rights reserved.
 */

import javax.xml.transform.Source;
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 {
    /**
     * Convert a W3C Document to a String.
     * 
     * Note: if you are working with a dom4j Document, you can use it's asXml() method.
     * 
     * @param doc
     *          org.w3c.dom.Document to be converted to a String.
     * @return String representing the XML document.
     * 
     * @throws TransformerConfigurationException
     *           If unable to get an instance of a Transformer
     * @throws TransformerException
     *           If the attempt to transform the document fails.
     */
    public static final StringBuffer docToString(final org.w3c.dom.Document doc)
            throws TransformerConfigurationException, TransformerException {

        StringBuffer sb = null;
        StringWriter writer = new StringWriter();

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(); // can throw
        // TransformerConfigurationException

        Source docSrc = new DOMSource(doc);
        t.transform(docSrc, new StreamResult(writer)); // can throw
        // TransformerException
        sb = writer.getBuffer();

        return sb;
    }
}