Wednesday, 24 April 2013

How to create a XML file dynamically using Java

Creating a XML file at RUNTIME using Java



package com.gaurav.xml.examples;

import java.io.BufferedReader;
import java.io.InputStreamReader;

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 CreateXmlFileDynamically {
                public static void main(String args[]) {
                                try {
                                                DocumentBuilderFactory docFactory = DocumentBuilderFactory
                                                                                .newInstance();
                                                DocumentBuilder builder = docFactory.newDocumentBuilder();
                                                Document document = builder.newDocument();
                                                BufferedReader br = new BufferedReader(new InputStreamReader(
                                                                                System.in));
                                                System.out
                                                                                .println("Enter the number of Element for XML files: - \n");
                                                int num = Integer.parseInt(br.readLine());
                                                System.out.println("Enter the root Element: \n");
                                                String rootElement = br.readLine();
                                                Element element = document.createElement(rootElement);
                                                document.appendChild(element);
                                                for (int i = 1; i <= num; i++) {
                                                                System.out.println("Enter the Element:- \n");
                                                                String strElement = br.readLine();
                                                                System.out.println("Enter the Data: - \n");
                                                                String strData = br.readLine();
                                                                Element childElement = document.createElement(strElement);
                                                                childElement.appendChild(document.createTextNode(strData));
                                                                element.appendChild(childElement);
                                                }
                                                TransformerFactory transFactory = TransformerFactory.newInstance();
                                                Transformer transformer = transFactory.newTransformer();
                                                DOMSource domSource = new DOMSource(document);
                                                StreamResult streamResult = new StreamResult(System.out);
                                                transformer.transform(domSource, streamResult);
                                } catch (Exception e) {
                                                System.out.println(e.getMessage());
                                }
                }
}

Result:-

 Note:- After the successful execution of above program, the final xml which we will get is as follows:-
             

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<student>
<studentId>1234</studentId>
<studentName>Arnold</studentName>
<birthDate>1985-11-26</birthDate>
</student>

1 comment:

  1. How to read the above dynamically created xml file using DOM?

    ReplyDelete