Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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

import org.w3c.dom.Document;

public class Main {
    public static void writeXmlToFile(Document doc, String filename) {
        File file = new File(filename);
        Result result = new StreamResult(file);
        writeXmlToStream(doc, result);

    }

    public static String writeXmlToStream(Document doc, Result streamResult) {
        String result = "";
        try {
            // Prepare the DOM document for writing
            Source source = new DOMSource(doc);

            // Write the DOM document to the file
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer xformer = tFactory.newTransformer();
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            xformer.transform(source, streamResult);
        } catch (TransformerConfigurationException e) {
        } catch (TransformerException e) {
        }
        return result;
    }
}