package com.alethis.xml;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class GenerateUsingDOM {

    private final static String OUTPUT_FILE = "C:\\project\\xml\\out.xml";
    
    public static final void main(String[] args) {
        try {
            Document doc = buildDocument();
            Source source = new DOMSource(doc);
            
            File f = new File(OUTPUT_FILE);
            FileWriter fw = new FileWriter(f);
            BufferedWriter bw = new BufferedWriter(fw);
            StreamResult result = new StreamResult(bw);            
            
            TransformerFactory tfactory = TransformerFactory.newInstance();
            Transformer transformer = tfactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");            
            transformer.transform(source, result);      
            
            System.out.println("DONE");
        }
        catch (FactoryConfigurationError fce) {
            System.out.println("Factory configuration error: " + fce.getMessage());
        }
        catch (ParserConfigurationException pce) {
            System.out.println("Parser configuration error: " + pce.getMessage());
        }
        catch (TransformerConfigurationException tce) {
            System.out.println("Transformer configuration error: " + tce.getMessage());
        }
        catch (TransformerException te) {
            System.out.println("Transformer error: " + te.getMessage());
        }
        catch (IOException ioe) {
            System.out.println("IO error: " + ioe.getMessage());
        }
    }

    private final static String FAMILY = "family";
    private final static String FATHER = "father";
    private final static String MOTHER = "mother";
    private final static String CHILDREN = "children";
    private final static String CHILD = "child";
    
    private final static String AGE = "age";
    
    private static Document buildDocument() throws FactoryConfigurationError, ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation implementation = builder.getDOMImplementation();
        Document doc = implementation.createDocument("", FAMILY, null);

        Node root = doc.getDocumentElement();

        Element father = doc.createElement(FATHER);
        father.setAttribute(AGE, "49");
        father.appendChild(doc.createTextNode("M. Boyle"));
        root.appendChild(father);

        Element mother = doc.createElement(MOTHER);
        mother.setAttribute(AGE, "46");
        mother.appendChild(doc.createTextNode("M. A. Maclure"));
        root.appendChild(mother);

        Element children = doc.createElement(CHILDREN);
        root.appendChild(children);

        Element chloe = doc.createElement(CHILD);
        chloe.setAttribute(AGE, "12");
        chloe.appendChild(doc.createTextNode("C. Boyle"));
        children.appendChild(chloe);

        Element adrian = doc.createElement(CHILD);
        adrian.setAttribute(AGE, "10");
        adrian.appendChild(doc.createTextNode("A. Boyle"));
        children.appendChild(adrian);
        
        return doc;
    }
}