Tuesday, July 26, 2011

Create XML File using Java (Source Code)

Hi All,
After so much on the XML front in my blog, I am going to put here the java source code which will create XML file with some dummy data. Guess, it should be the first blog of XML. Nevertheless,  better late than never.

Simple Java Code to create one XML file:
API used:

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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.Document;
import org.w3c.dom.Element;

public class XMLCreation {

      public static void main(String args[]) {

            XMLCreation xmlCreate = new XMLCreation();
            xmlCreate.createXML();
      }

      private void createXML() {

            String customerName = "Ramesh";
            String customerAddress = "Delhi";

            DocumentBuilder domBuilder = null;
            try {
                  DocumentBuilderFactory domFactory = DocumentBuilderFactory
                              .newInstance();
                  domBuilder = domFactory.newDocumentBuilder();
            } catch (ParserConfigurationException pcEx) {
                  System.out
                              .println("ParserConfiguration Exception "
                                          + pcEx.getMessage());
            } catch (Exception e) {
                  System.out
                              .println("Exception Occured "
                                          + e.getMessage());
            }
            Document newDoc = domBuilder.newDocument();

            // Root element
            Element rootElement = newDoc.createElement("CustomerInformation");
            newDoc.appendChild(rootElement);

            // Creating element containing value of Name
            Element curElement = newDoc.createElement("Name");
            curElement.appendChild(newDoc.createTextNode(customerName));
            rootElement.appendChild(curElement);

            // Creating element signifies address
            keyElement.appendChild(newDoc.createTextNode(customerAddress));
            rootElement.appendChild(keyElement);

            DOMSource sourceInt = new DOMSource(newDoc);


            File stew = new File("C:/sampleXMLfile.xml");

            StreamResult resultInt = new StreamResult(stew);

            TransformerFactory tFactoryInt = TransformerFactory.newInstance();
            Transformer transformerInt = null;

            try {
                  transformerInt = tFactoryInt.newTransformer();
            /* Transformation done here, which actually does the file creation*/   
transformerInt.transform(sourceInt, resultInt);
            } catch (TransformerException tEx) {
                  System.out
                              .println("TransformerException Occured "
                                          + tEx.getMessage());
            } catch (Exception e) {
                  System.out
                              .println("Exception Occured"
                                          + e.getMessage());
            }

      }
}

No comments:

Total Pageviews