Home Xml Dom Create - XML File (Document)
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Create - XML File (Document)

Advertisement
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 Java File: CreatXMLFile.java

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
 
public class CreatXMLFile {
  public static void main(String[] argsthrows Exception {
  BufferedReader bf = new BufferedReader(
  new 
InputStreamReader(System.in));
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 = 
  DocumentBuilderFactory.newInstance
();
  DocumentBuilder documentBuilder = 
 documentBuilderFactory.newDocumentBuilder
();
  Document document = documentBuilder.newDocument();
  Element rootElement = document.createElement(root);
  document.appendChild(rootElement);
  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);
  em.appendChild(document.createTextNode(data));
  rootElement.appendChild(em);
  }
  TransformerFactory transformerFactory = 
  TransformerFactory.newInstance
();
  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>javac CreatXMLFile.java

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>
<Emp-Name>Vino
d</Emp-Name><Emp-Code>E-001</Emp-Code>
<Emp-Desi>Junior-Programmer</Emp-Desi></Ro
nseIndia>
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

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

Post your Comment


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

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

thanks alot, its really nice example.it 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.
K.R.Raja
November 5, 2011
xml file down load

java aplication
sathyakumari
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?
Shantanu
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.
DMCA.com