Wednesday, June 15, 2011

Simple DOM example (Java)

The following is a simple example of the DOM parser, which creates an XML document and outputs the document to the console.

 package src;

import java.io.PrintWriter;

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

    public static void main(String[] args){
        DocumentBuilder documentBuilder;
        try {
            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = documentBuilder.newDocument();
            Element root = document.createElement("root");
            Element text1 = document.createElement("text");
            text1.appendChild(document.createTextNode("hi"));
            root.appendChild(text1);
            Element text2 = document.createElement("text");
            text2.appendChild(document.createTextNode("bye"));
            root.appendChild(text2);
            document.appendChild(root);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource source = new DOMSource(document);
            PrintWriter writer = new PrintWriter(System.out);
            StreamResult result = new StreamResult(writer);
            transformer.transform(source , result);
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }
}

Output of the above code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><text>hi</text><text>bye</text></root>

Firstly a org.w3c.dom.Document is created,
            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = documentBuilder.newDocument();

Then two elements, namely 'text' are created and appended to the root element, 'root'. Then the root element is appended to the document as follows;
            Element root = document.createElement("root");
            Element text1 = document.createElement("text");
            text1.appendChild(document.createTextNode("hi"));
            root.appendChild(text1);
            Element text2 = document.createElement("text");
            text2.appendChild(document.createTextNode("bye"));
            root.appendChild(text2);
            document.appendChild(root);

Finally the document (created XML is output to the console). DomSource is created with the document. StreamResult is created wrapping a PrintWritter, which outputs to the console, System.out.
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource source = new DOMSource(document);
            PrintWriter writer = new PrintWriter(System.out);
            StreamResult result = new StreamResult(writer);
            transformer.transform(source , result);

Thanks,
Shyarmal.

No comments:

Post a Comment