Create - XML File (Document)

In this section, you will learn to create a XML document using the DOM APIs.

Create - XML File (Document)


In this section, you will  learn to create a XML document using the DOM APIs. This XML document uses  1.0 version  and UTF-8 encoding. 

Description of program:

This program helps in creating a XML document on the console. This program asks for the number of elements to be added in the generated xml file. It takes the root name at the console and passes it in the createElement() method. It creates the Element object and invokes the Document object  . Depending upon the given number, it creates that much elements and fills them with data,. Finally, it displays the generated XML file with its version and encoding.

Here is the video insturction "How to create XML in Java?":

Here is Java File:

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
public class CreatXMLFile {
  public static void main(String[] argsthrows Exception {
  BufferedReader bf = new BufferedReader(
System.out.print("Enter number to add elements in your XML file: ");
  String str = bf.readLine();
  int no = Integer.parseInt(str);
  System.out.print("Enter root: ");
  String root = bf.readLine();
  DocumentBuilderFactory documentBuilderFactory = 
  DocumentBuilder documentBuilder = 
  Document document = documentBuilder.newDocument();
  Element rootElement = document.createElement(root);
  for (int i = 1; i <= no; i++){
  System.out.print("Enter the element: ");
  String element = bf.readLine();
  System.out.print("Enter the data: ");
  String data = bf.readLine();
  Element em = document.createElement(element);
  TransformerFactory transformerFactory = 
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(document);
  StreamResult result =  new StreamResult(System.out);
  transformer.transform(source, result);

Download this example.

Output of this program:


C:\vinod\xml>java CreatXMLFile
Enter number to add elements in your XML file: 3
Enter root: RonseIndia
Enter the element: Emp-Name
Enter the data: Vinod
Enter the element: Emp-Code
Enter the data: E-001
Enter the element: Emp-Desi
Enter the data: Junior-Programmer
<?xml version="1.0" encoding="UTF-8" standalone="no"?><RonseIndia>


Download source code of the project in Eclipse Project format

Share on Google+Share on Google+

Create - XML File (Document)

Posted on: June 5, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Create - XML File (Document)   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
sai prasad
June 7, 2011
stax parser

how to implement the same javaprogram with stax parser
September 7, 2011

thanks alot, its really nice helped me alot.
Md. Abu Saif
September 14, 2011
create xml file

this code works. but if you give me some example about how to write those xml data in a xml file ,it will help a lot.thanks.
November 5, 2011
xml file down load

java aplication
November 12, 2011
XML File generation

I went with the java code and it is very helpful for my current project. I have some other questions here in my current project, Java file needs to read from a configfile.xml which has all tags with constant values and some coming from database tables and i need to generate it as again a XML file. Can you help me in this case?
December 8, 2011
Code is fine

Thanks a lot, the code is working absolutly fine. nice upload.
Indrajit sarkar
September 5, 2012
How to create xml file my computer?

please mail thowroly above subject.
Shruthi S
September 11, 2012
Creating an XML File using Java

I was able to print it into a file but this is not in the proper order. I mean the whole bunch of text is in the same line. How to print this line by line.