Wednesday, July 6, 2011

How To Create XML File in JAVA - (DOM)

0 comments
In Java, we can create XML by using DOM class.


Example :


This is the XML format you  create latter




<?xml version="1.0" encoding="UTF-8" ?>
 <order>
 <orderDetails>
  <waiterName>Siridasa</waiterName>
  <kitchName>Chines Kitchen</kitchName>
  <tableName>20</tableName>
  </orderDetails>
<item>
  <itemid>ARR01</itemid>
  <itemName>DCL White Arak</itemName>
  <itemQty>1</itemQty>
  </item>
 <item>
  <itemid>ARR02</itemid>
  <itemName>Lemon Gin</itemName>
  <itemQty>2</itemQty>
  </item>
 <item>
  <itemid>ARR03</itemid>
  <itemName>Ritz</itemName>
  <itemQty>10</itemQty>
  </item>
  </order>


package jsupport.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com
 */
public class CreateXML {

    DocumentBuilderFactory documentBuilderFactory;
    DocumentBuilder documentBuilder;
    Document document;

    public void createXML(Map order_details, ArrayList item_details) {

        Set set;
        Iterator iterator;

        try {
//              Creating an empty XML Document

            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.newDocument();

//               Creating XML tree
//               create the root element and add it to the document

            Element orderElement = document.createElement("order");
            document.appendChild(orderElement);

            Element orderDetailElement = document.createElement("orderDetails");
            orderElement.appendChild(orderDetailElement);

            set = order_details.keySet();
            iterator = set.iterator();

//                create child element acording to the key set,add an attribute, and add to root (orderDetails)

            while (iterator.hasNext()) {

                String key = (String) iterator.next();
                Element detailElement = document.createElement(key);
                detailElement.appendChild(document.createTextNode((String) order_details.get(key)));
                orderDetailElement.appendChild(detailElement);

            }

            for (int i = 0; i < item_details.size(); i++) {

//                  create item element and add item details as child

                Element itemElement = document.createElement("item");
                orderElement.appendChild(itemElement);

                Element itemId;
                String itemdetails[] = (String[]) item_details.get(i);


                itemId = document.createElement("itemid");
                itemId.appendChild(document.createTextNode(itemdetails[0]));

                itemElement.appendChild(itemId);
                itemId = document.createElement("itemName");
                itemId.appendChild(document.createTextNode(itemdetails[1]));

                itemElement.appendChild(itemId);
                itemId = document.createElement("itemQty");
                itemId.appendChild(document.createTextNode(itemdetails[2]));
                itemElement.appendChild(itemId);

            }

//              create XML file


            File xmlFile = new File("C:\\Test.xml");
            xmlFile.createNewFile();
            FileOutputStream isod = new FileOutputStream(xmlFile);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();

            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(isod);
            transformer.transform(source, result);

            isod.flush();
            isod.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (TransformerException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {


        CreateXML cXML = new CreateXML();
        Map order_details = new HashMap();
        ArrayList item_details = new ArrayList();
        String itemDetails[] = new String[3];
        String itemDetails2[] = new String[3];
        String itemDetails3[] = new String[3];

//     add order details
        order_details.put("kitchName", "Chines Kitchen");
        order_details.put("waiterName", "Siridasa");
        order_details.put("tableName", "20");

//     add item details
        itemDetails[0] = "ARR01";
        itemDetails[1] = "DCL White Arak";
        itemDetails[2] = "1";
        item_details.add(itemDetails);

        itemDetails2[0] = "ARR02";
        itemDetails2[1] = "Lemon Gin";
        itemDetails2[2] = "2";
        item_details.add(itemDetails2);

        itemDetails3[0] = "ARR03";
        itemDetails3[1] = "Ritz";
        itemDetails3[2] = "10";
        item_details.add(itemDetails3);

        cXML.createXML(order_details, item_details);

    }
}

Read more...