How to create XML File In Java

This is a example of creating XML file in java application.

The Main Class Code

import java.io.File;
import java.util.ArrayList;

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



public class CreateXml {

    public static void main(String[] args) throws TransformerException {
        ArrayList<String> arrayOne = new ArrayList<String>();
        ArrayList<String> arrayTwo = new ArrayList<String>();
        ArrayList<String> arrayThree = new ArrayList<String>();
       
        for(int i = 0; i < 20; i++){
            arrayOne.add("Element : " + Integer.toString(i));
            arrayTwo.add("Element : " + Integer.toString(i));
            arrayThree.add("Element : " + Integer.toString(i));
        }       

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        try {
            docBuilder = docFactory.newDocumentBuilder();

            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("Root");
            doc.appendChild(rootElement);

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

                // elements
                Element staff = doc.createElement("Value");
                rootElement.appendChild(staff);

                // set attribute to element
                Attr attr = doc.createAttribute("id");
                attr.setValue(arrayThree.get(i).toString());
                staff.setAttributeNode(attr);

                Element tag1 = doc.createElement("tag1");
                tag1.appendChild(doc.createTextNode(arrayOne.get(i).toString()));
                staff.appendChild(tag1);

                Element tag2 = doc.createElement("tag2");
                tag2.appendChild(doc.createTextNode(arrayTwo.get(i).toString()));
                staff.appendChild(tag2);

            }

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer;
            try {
                transformer = transformerFactory.newTransformer();

                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File("d:\\developersnoteXMLFile.xml"));

                // Output to console for testing
                // StreamResult result = new StreamResult(System.out);
                transformer.transform(source, result);

                System.out.println("File saved!");
            } catch (TransformerConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}


Sample Output

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Root>
  <Value id="Element : 0">
    <tag1>Element : 0</tag1>
    <tag2>Element : 0</tag2>
  </Value>
  <Value id="Element : 1">
    <tag1>Element : 1</tag1>
    <tag2>Element : 1</tag2>
  </Value>
  <Value id="Element : 2">
    <tag1>Element : 2</tag1>
    <tag2>Element : 2</tag2>
  </Value>
  <Value id="Element : 3">
    <tag1>Element : 3</tag1>
    <tag2>Element : 3</tag2>
  </Value>
  <Value id="Element : 4">
    <tag1>Element : 4</tag1>
    <tag2>Element : 4</tag2>
  </Value>
  <Value id="Element : 5">
    <tag1>Element : 5</tag1>
    <tag2>Element : 5</tag2>
  </Value>
  <Value id="Element : 6">
    <tag1>Element : 6</tag1>
    <tag2>Element : 6</tag2>
  </Value>
  <Value id="Element : 7">
    <tag1>Element : 7</tag1>
    <tag2>Element : 7</tag2>
  </Value>
  <Value id="Element : 8">
    <tag1>Element : 8</tag1>
    <tag2>Element : 8</tag2>
  </Value>
  <Value id="Element : 9">
    <tag1>Element : 9</tag1>
    <tag2>Element : 9</tag2>
  </Value>
  <Value id="Element : 10">
    <tag1>Element : 10</tag1>
    <tag2>Element : 10</tag2>
  </Value>
  <Value id="Element : 11">
    <tag1>Element : 11</tag1>
    <tag2>Element : 11</tag2>
  </Value>
  <Value id="Element : 12">
    <tag1>Element : 12</tag1>
    <tag2>Element : 12</tag2>
  </Value>
  <Value id="Element : 13">
    <tag1>Element : 13</tag1>
    <tag2>Element : 13</tag2>
  </Value>
  <Value id="Element : 14">
    <tag1>Element : 14</tag1>
    <tag2>Element : 14</tag2>
  </Value>
  <Value id="Element : 15">
    <tag1>Element : 15</tag1>
    <tag2>Element : 15</tag2>
  </Value>
  <Value id="Element : 16">
    <tag1>Element : 16</tag1>
    <tag2>Element : 16</tag2>
  </Value>
  <Value id="Element : 17">
    <tag1>Element : 17</tag1>
    <tag2>Element : 17</tag2>
  </Value>
  <Value id="Element : 18">
    <tag1>Element : 18</tag1>
    <tag2>Element : 18</tag2>
  </Value>
  <Value id="Element : 19">
    <tag1>Element : 19</tag1>
    <tag2>Element : 19</tag2>
  </Value>
</Root>


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example