Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayOutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static String getContent(Node node, boolean omitXMLDeclaration) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.setOutputProperty("encoding", "UTF-8");
            if (omitXMLDeclaration) {
                transformer.setOutputProperty("omit-xml-declaration", "yes");
            }

            DOMSource source = new DOMSource(node);
            StreamResult result = new StreamResult(baos);
            transformer.transform(source, result);

            String cont = baos.toString("UTF8");

            baos.close();
            return cont;
        } catch (Exception ex) {
            return "";
        }
    }
}