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

public class Main {
    private static Transformer trans;

    public static String xmlToString(Document xml) throws TransformerConfigurationException, TransformerException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Transformer transformer = getTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(new DOMSource(xml), new StreamResult(bos));
        return bos.toString();
    }

    public static Transformer getTransformer() throws TransformerConfigurationException {
        if (trans == null) {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            trans = tFactory.newTransformer();
        }
        return trans;
    }
}