Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners xml creation in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Valli
xml creation in java
1 Answer(s)      4 years 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


Advertisement
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 Tutorials/Questions & Answers:
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
Advertisements
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
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 - 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 table using a Java swing
creation of table using a Java swing  how to create a table dynamically in Java swing
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
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
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
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
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
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 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
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
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
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 can write data into XML file using DOM parser? How can convert HTWL file to XML file
java - XML
java  how can i validate my xml file using java code plz send me de... kumar  Hi friend, Step to validate a xml file against a DTD (Document Type Definition) using the DOM APIs. Here is the xml file "User.xml
XML and java
XML and java  Hi I have xml data in XMLStreamreader object how to retrive tha data and write into a file using java Your help will be much appreciated Thanks   Hi Friend, Try the following code: import java.io.
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
java - XML
you get the data3. And in the servlet class you can use the DOM api to create xml... it to the xml treedoc.appendChild(root);For more information please visit http://www.roseindia.net/xml/dom/createdomchildelement.shtmlThanks
Advertisements
 

 

 

DMCA.com