Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 Copyright 2014 Michael K Martin
    
 This file is part of Civet.
    
 Civet is free software: you can redistribute it and/or modify
 it under the terms of the Lesser GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 Civet 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 Lesser GNU General Public License
 along with Civet.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;

public class Main {
    public static String getXMLString(Document dom, boolean bOmitDeclaration) {
        return getXMLString(dom, bOmitDeclaration, null);
    }

    public static String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) {
        String sOmit = (bOmitDeclaration ? "yes" : "no");
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            if (sEncoding != null)
                transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding);
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit);
            transformer.transform(new DOMSource(dom), new StreamResult(buffer));
            return buffer.toString();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
}