Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.FileWriter;

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.Document;

import org.w3c.dom.Element;

public class Main {
    public static void write(Element root, String xmlPath) {
        try {
            Document doc = root.getOwnerDocument();
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty("encoding", "utf-8");

            FileWriter fw = new FileWriter(xmlPath);

            transformer.transform(new DOMSource(doc), new StreamResult(fw));

            fw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}