Saturday, January 23, 2010

Dom Java Example

Here we can see how to create XML file using Document object Model in java.
         Below programs explains in detail.you can modify the generic terms in this example into as per your requirements. 
Below example explains the write operation.
Example

       

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class DomExample {

    private String OutputFile = null;
    private Document document_ = null;

    public DomExample() {
        File outFile = new File(
                "E:\\Test\\OutputFile.xml");
        OutputFile = outFile.getAbsolutePath();
        createDoc(OutputFile);
        addHeader(document_);
        addContent(document_);
        WriteToFile(document_, OutputFile);
    }

    private void addContent(Document document) {

        Element root = document.getDocumentElement();

        Element mainContentontechildElement = document
                .createElement("ContentStartHere");

        Element FirstContentchildElement = document.createElement("Content1");
        Text ele = document.createTextNode("1");
        FirstContentchildElement.appendChild(ele);

        Element SecContentchildElement = document.createElement("Content2");
        Text vntext = document.createTextNode("2");
        SecContentchildElement.appendChild(vntext);

        mainContentontechildElement.appendChild(FirstContentchildElement);
        mainContentontechildElement.appendChild(SecContentchildElement);

        root.appendChild(mainContentontechildElement);

    }

    private void WriteToFile(Document document, String outputFile) {

        TransformerFactory transformerFactory = null;
        Transformer transformer = null;

        Source source = null;
        Result output = null;
        transformerFactory = TransformerFactory.newInstance();

        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            System.out.println(e.getMessage());

        }

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF8");
        Node doc;
        source = new DOMSource(document);
        File filetemp = null;
        try {
            filetemp = new File(outputFile);
            output = new StreamResult(filetemp);
            transformer.transform(source, output);

        } catch (TransformerException e) {
            System.out.println(e.toString());
        }

    }

    public void addHeader(Document doc) {

        Element root = doc.getDocumentElement();

        root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
                "http://t/namespace/");

        root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi",
                "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:schemaLocation",
                "http://www.test.com/namespace xsdFile.xsd");
        Element mainchildElement = doc.createElement("Header");

        Element FirstchildElement = doc.createElement("value1");
        Text ele = doc.createTextNode("1");
        FirstchildElement.appendChild(ele);

        Element SecchildElement = doc.createElement("value2");
        Text vntext = doc.createTextNode("2");
        SecchildElement.appendChild(vntext);

        mainchildElement.appendChild(FirstchildElement);
        mainchildElement.appendChild(SecchildElement);

        root.appendChild(mainchildElement);
    }

    private Document createDoc(String outFile) {

        DocumentBuilderFactory factory = null;
        DocumentBuilder builder = null;
        DOMImplementation dImpl = null;

        try {
            factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
            dImpl = builder.getDOMImplementation();

            document_ = dImpl.createDocument(null, "rootElementHere", null);

        }

        catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        }

        return document_;

    }

    public static void main(String[] args) {
        new DomExample();
    }
}


                     

0 Comments:

Post a Comment

The Link Exchange - Your ultimate resource for link exchange!

About This Blog

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP