Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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;

import org.w3c.dom.Text;

public class Main {
    public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
            String pathSaveFile) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
            // creating a new instance of a DOM to build a DOM tree.
            Document doc = docBuilder.newDocument();

            Element root = doc.createElement(rootElement);
            // adding a node after the last child node of the specified node.
            doc.appendChild(root);

            Element interfaceElement = null;

            Element child = null;
            Text text;

            for (Object obj : listObject) {
                Class srcClass = obj.getClass();
                Field[] field = srcClass.getFields();
                interfaceElement = doc.createElement(interfaceName);
                for (int i = 0; i < field.length; i++) {
                    // System.out.println(field[i].getName() + ":" +
                    // field[i].get(obj));
                    child = doc.createElement(field[i].getName());

                    text = doc.createTextNode((field[i].get(obj)).toString());
                    child.appendChild(text);

                    interfaceElement.appendChild(child);
                }
                root.appendChild(interfaceElement);
            }

            // TransformerFactory instance is used to create Transformer
            // objects.
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            // create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);
            String xmlString = sw.toString();

            File file = new File(pathSaveFile);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write(xmlString);
            bw.flush();
            bw.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}