Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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>

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (post your own) View All Comments Latest 10 Comments:

This is one of the best websites to learn JAVA. Every line of code is explained in a very simple manner.

Posted by Ajay Anand on Tuesday, 01.8.08 @ 16:00pm | #44791

I got the following error while compiling the code. "DOMSource node as this type not supported."

Please advise!

Posted by Sylvia on Saturday, 12.15.07 @ 05:37am | #42377

I am new to both Java&XML, this site it the best I found so far. Thanks!

Posted by Y on Friday, 11.9.07 @ 00:32am | #36748

I searched for DOM answers all over the place until I found this tutorial. Clear, easy to understand with simple examples. Thanks!!

Posted by anon on Tuesday, 08.7.07 @ 23:02pm | #22890

the examples were very good and well explained. and they worked. liked this site very much. will visit again.

Posted by mohit on Saturday, 07.21.07 @ 15:34pm | #21691

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.