Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.io.StringWriter;

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 {
    public static String constructXMLString(Document dom) throws IOException {
        String retXMLStr = null;
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = factory.newTransformer();
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            DOMSource source = new DOMSource(dom);
            transformer.transform(source, result);
            writer.close();
            retXMLStr = writer.toString();
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*
        // Make a string out of the DOM object
        System.out.println(dom.toString());
        OutputFormat format = new OutputFormat(dom);
        format.setIndenting(true);
        ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream();
        XMLSerializer serializer = new XMLSerializer(byteoutStream, format);
        serializer.serialize(dom);
        String retXMLStr = new String(byteoutStream.toByteArray());
        byteoutStream.close();
         */
        return retXMLStr;

    }
}