Home Answers Viewqa Java-Beginners xml creation in java

 
 


Valli
xml creation in java
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners

HI,

I need a java program to create an xml file... therez a tutorial in your site to create an xml file at

http://www.roseindia.net/xml/dom/CreatXMLFile.shtml

but this isn't creating a file, its just displaying the output on console. I want an xml file abc.xml to be created with he program...

can you pls help...

Thanq in advance

View Answers

November 19, 2010 at 12:22 PM


Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class CreateXML{
public static void createXmlFile(Document doc,String name,String address,String contactNo,String email) throws Exception {
        Element root = doc.createElement("Employee");
        doc.appendChild(root);
        Element element1 = doc.createElement("Emp_Name");
        root.appendChild(element1);
        Text text1 = doc.createTextNode(name);
        element1.appendChild(text1);

        Element element2 = doc.createElement("Emp_Address");
        root.appendChild(element2);
        Text text2 = doc.createTextNode(address);
        element2.appendChild(text2);

        Element element3 = doc.createElement("Emp_ContactNo");
        root.appendChild(element3);
        Text text3 = doc.createTextNode(contactNo);
        element3.appendChild(text3);

        Element element4 = doc.createElement("Email");
        root.appendChild(element4);
        Text text4 = doc.createTextNode(email);
        element4.appendChild(text4);

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File("c:/employee.xml");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
      }
      public static void main(String args[])throws Exception{
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Scanner input=new Scanner(System.in);
        System.out.print("Enter Employee Name:");
        String name=input.nextLine();
        System.out.print("Enter Address:");
        String address=input.nextLine();
        System.out.print("Enter Contact No:");
        String contactNo=input.nextLine();
        System.out.print("Enter Email:");
        String email=input.nextLine();
        createXmlFile(doc,name,address,contactNo,email);
        System.out.println("Xml File is Created Successfully");
    }
}

Thanks









Related Pages:
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml
xml creation in java
xml creation in java  HI, I need a java program to create an xml file... therez a tutorial in your site to create an xml file at http://www.roseindia.net/xml/dom/CreatXMLFile.shtml but this isn't creating a file, its just
Creation of xml
Creation of xml  Hi, I need to fetch details from my database and to create a xml file containing all those datas...My database datas are in key... of query select * from tablename where appid="+12345+". An xml file should
reports creation
reports creation  hi................. how to create tabular format report in java swings?????????????   Please visit the following link: http://www.roseindia.net/java/example/java/swing
object creation
object creation  when will java object is created? is it at runtime or compiletime
package creation
package creation  program to create package having four different class in java
Image_creation
Image_creation  how to create an image from a set of pixel values stored in array - in java
Table Creation
Table Creation  hi! I want a table which looks like following can u tell me how it can be made with java swings.. YEAR 2005 Jan Feb Mar Apr May Jun July Aug Sep pH Phosphate Chloride
object creation - Java Beginners
object creation  I need object creation in depth(with stack,pc registers).Any one can provide me if possible with video/audio
creation of installer - Java Magazine
creation of installer  plz tell me how can be create installer for any developed application in java?  visit the following url izpack.org.. it will helps u
object creation - Subversion
object creation in Java  In how many ways we can create objects in Java
Spring Bean Example, Spring Bean Creation
Basic Bean Creation The Spring bean can typically be POJO(Plain Old Java... java bean file having two methods getMessage() and setMessage() and a default... is a standard Java application with a main method. A BeanFactory will load bean
creation of table using a Java swing
creation of table using a Java swing  how to create a table dynamically in Java swing
exe file creation - JDBC
exe file creation   hi i have done a project in java swings.project name is format migrator.means db migrator. now my aim is create EXE FILE for my project. pls do consider
bean creation exception
bean creation exception  hi i am getting exception while running simple spring ioc program Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class
XSD Creation. - WebSevices
XSD Creation.  When I try to generate xsd from java object... with annotation in java object but I am not getting the desired output(order which is given in the java objects) Do we need to change any properties
PDF creation in JAVA - JSP-Servlet
PDF creation in JAVA  HI! Good morning.... I want to create pdf file and i want to write something into pdf file....before creation. Upto creation i have done but how to write data into pdf. File Writer is not working
cache creation in java - Java Beginners
cache creation in java  Hi All, I am planing to create cache in my web application. please send me the some sample code to create cache by using org.apache.oro.utils package. please help me out. Thanks, mln15584
Random Creation of password
Random Creation of password  Dear Sir I have created a form with some details in it.When clicking on the submit button it gives me a password... to be encrypted and then send to the database.....I am using jsp as my front end and java
creation an dmanipulation of trees - Java Beginners
creation an dmanipulation of trees  Hi guys? pls i want to find... a Binary Search Tree -To use the Java API's Stack in conjunction with other.../java/example/java/swing/index.shtml Thanks
xml
xml  how to creatte html file and validate using java and finally i need get web.xml file
XML - XML
XML XSD validation in java  Can anyone help in writing validation for XML XSD in Java
xml - XML
xml  how to match to xml file?can you give example with java code
website creation using java and mysql - Java Beginners
website creation using java and mysql  i am going to create a website for some company using java and mySql... i dont know the procedure to create a website.can anybody give me the steps what i have to follow
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
Core java interview question, object creation.
Core java interview question, object creation.  How can we restrict to create objects for more than five? That means i want only 5 objects, how to restrict to create 6th objects
java-jar file creation - Java Beginners
java-jar file creation  how to create a jar file?i have a folder... file  Hi Friend, JAR stands for the Java Archive. This file format is used to distribute a set of java classes. This file helps you to reduce the file
Cache creation using java - Java Beginners
Cache creation using java  Hi All, I am developing a web application, I have a plan to use cache in that application. I used jsp caching with the help of jsp taglib. But I want to write code in a java class,not in the jsp
Creation of MultiThreads
Creation of Multiple Threads       Like creation of a single thread, You can also... C:\nisha>java MultiThread1 Thread Name :main Thread Name :My
Creation of MultiThreads
Creation of MultiThreads       Like creation of a single thread, You can also create more than one...;java MultiThread1 Thread Name :main Thread Name :My Thread 1
jar File creation - Swing AWT
in java but we can create executable file in java through .jar file.. how can i convert my java file to jar file? Help me
java and xml - XML
java and xml  Hi Deepak, I want learn xml and java(applications). Which editor is best usefull(trial--version) in my applications...; Hi friend, http://www.roseindia.net/xml/dom/ Thanks
Jar file creation - Swing AWT
this? 2. To run a java swing application what are the necesseray things the clients
Problem in EJB application creation - EJB
. Deployment error: The Sun Java System Application Server could not start. More
Java + XML - XML
Java + XML  1) I have some XML files, read one xml... java...the attribute value..not sure now how to read the xml file passing..." pointing to a hello xml file ..read that file and get the value of the parent
Java-XML-DOM - XML
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
cookie creation without specifying the vaue - Java Beginners
cookie creation without specifying the vaue  how do we create cookies using java servlets by just specifying the name and not providing the value for it.because my colleague said once cookie is created value will be assigned
java and xml - XML
java and xml   test_final_1 2009-025T13:23:45 B2B I want to validate each tag.. and i don't have any xsd.. kindly help me to solve...://www.roseindia.net/xml/dom/DOMValidateDTD.shtml Thanks
XML in java - XML
XML in java  Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them...://www.roseindia.net/xml/sax/SAXElementCount.shtml Hope that it will be helpful
XML with JAVA - XML
document and the processing in JAVA program . Find the time for each XML element...XML with JAVA  Hi.. This is Priya here. Thanks for responding me. I have a query. "Write program in java to evaluate the time to access
java and xml problem - XML
java and xml problem  hi, i need to write a java program that generates an xml file as follows: aaa vvv --------- i have witten a program in java, but im
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
java - XML
java  How can write data into XML file using DOM parser? How can convert HTWL file to XML file
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have only one element with multiple attributes with same tag name. here is my file... a solution.my java code is given below. import java.io.File; import
Limit the Class Object Creation
to restrict the object creation of a class. This is a simple code in Java where I have...Limit the Class Object Creation In this section you will learn about how a class object creation can be a restricted to a fix number of times
java - XML
This is my XML file then how can read this XML file Using DOM & SAX parsers in java? How can write the same data into XML file using DOM parser? Could you
java with xml
java with xml  hi i have a problem. // this sample code is reading xml file in java DefaultHandler handler = new DefaultHandler... xml file change in future then my java code will also change. but is there any
java - XML
java  How to prepare an xml document by reading data from a table in the database . (the database access logic is written inside the servlet

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.