Wednesday 24 April 2013

How to create a XML file dynamically using Java

Creating a XML file at RUNTIME using Java



packagecom.gaurav.xml.examples;

importjava.io.BufferedReader;
importjava.io.InputStreamReader;

importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.transform.Transformer;
importjavax.xml.transform.TransformerFactory;
importjavax.xml.transform.dom.DOMSource;
importjavax.xml.transform.stream.StreamResult;

importorg.w3c.dom.Document;
importorg.w3c.dom.Element;

public classCreateXmlFileDynamically {
                public static void main(String args[]) {
                                try {
                                                DocumentBuilderFactory docFactory = DocumentBuilderFactory
                                                                                .newInstance();
                                                DocumentBuilder builder = docFactory.newDocumentBuilder();
                                                Document document = builder.newDocument();
                                                BufferedReader br = newBufferedReader(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 (inti = 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>

No comments:

Post a Comment