Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.OutputStream;
import java.io.PrintWriter;

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

public class Main {
    public static String toString(Document doc) throws TransformerException {

        OutputStream result = new ByteArrayOutputStream();

        transfer(doc, result);

        return result.toString();
    }

    public static void transfer(Document doc, OutputStream result)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

        transfer(doc, new StreamResult(result));
    }

    public static void transfer(Document doc, PrintWriter pw) throws TransformerException {

        transfer(doc, new StreamResult(pw));
    }

    private static void transfer(Document doc, Result streamResult)
            throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

        Source source = new DOMSource(doc);
        TransformerFactory xformFactory = TransformerFactory.newInstance();
        Transformer idTransform = xformFactory.newTransformer();
        idTransform.transform(source, streamResult);
    }
}