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



Home Answers Viewqa Java-Beginners Writing xml file
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


santosh
Writing xml file
1 Answer(s)      6 years ago
Posted in : Java Beginners



Advertisement
View Answers

November 19, 2008 at 11:53 PM


Hi friend,

Code to solve the problem :

import java.io.*;
import java.sql.*;

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

public class JavaXmlDataBase{
public static void main(String args[]) throws IOException {

try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
//creating a new instance of a DOM to build a DOM tree.
Document doc = docBuilder.newDocument();
new XmlServlet().createXmlTree(doc);

System.out.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}


}

public void createXmlTree(Document doc) throws Exception {
//This method creates an element node
Element root = doc.createElement("Company");
//adding a node after the last child node of the specified node.
doc.appendChild(root);

Element child = doc.createElement("Location");
root.appendChild(child);


Element child1 = doc.createElement("Companyname");
child.appendChild(child1);

Text text = doc.createTextNode("Roseindia .Net");
child1.appendChild(text);

Comment comment = doc.createComment("Employee in roseindia");
child.appendChild(comment);


Connection con = null;
int count = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_register","root","root";);
try{
String sql = "SELECT * FROM employee_details";
PreparedStatement prest = con.prepareStatement(sql);


ResultSet rs = prest.executeQuery();
Element element=null;
Text text1=null;
while (rs.next()){
String name = rs.getString(2) + " " + rs.getString(3);

element = doc.createElement("Employee");
child.appendChild(element);

text1 = doc.createTextNode(name);
element.appendChild(text1);
count++;
}
prest.close();
con.close();
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}







//TransformerFactory instance is used to create Transformer objects.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

// create string from xml tree
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:/newxml.xml");
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();

}
}

For more information on Java visit to :

http://www.roseindia.net/java/

Thanks


Related Tutorials/Questions & Answers:
Writing xml file - Java Beginners
Writing xml file  Thank you for the quick response The values which... XmlServlet().createXmlTree(doc); System.out.println("Xml File Created... from xml tree StringWriter sw = new StringWriter
java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my... xml file and storet that in particlar location. Please help me out Thanks
Advertisements
File saving and Writing
File saving and Writing   Hello Rose india..... I have a doubt... a "file create option" it will be ".csv" file and i want to write the data from arraylist into .csv file and i want to save that file so it will ask for "save
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed... (FirstName, LastName, Age). I also made an empty file I named "contactsFile.txt" where
writing and appending to a file
writing and appending to a file  My input file includes data from... and values and writes into an existing file for 'male'. How can I do the same..." + " :class :" + Name);// appends the string to the file
File Writing - Java Beginners
File Writing  Hi... I need a syntax or logic or program by which we can write into the desired shell of the Excel file from console window...  Hi friend, I am sending you a link. This is will help you. Please
xml file display - XML
xml file display   - - - - ADL SCORM CAM 1.3 - - session3 - Online Instructional Strategies that Affect Learner... code to display the above xml file in tree structure where
Sorting Xml file - XML
Sorting Xml file  I have an xml file like this: Smith USA... sort my xml file. Here is my xslt..., my ouput file will be like this: Amy AUC Bob USA John UK
splitting large xml file. - XML
splitting large xml file.  Hi, I have a large xml file(1G) and a schema (XSD) i need to split this xml to small xml files (~20M) that will be valid by the schema
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file  Can any one please correct this code. This is to read a file and later...-code/16483-counting-values-input-file-writing-output-file.html this code
executio of xml file
executio of xml file  how to execute xml file , on web browser
How to create XML file - XML
How to create XML file  Creating a XML file, need an example. Thanks!!  To create XML file you need plain text editor like note pad. After creating your file save it with .xml extension.  Hi,Java programming
Writing Log Records to a Log File
Writing Log Records to a Log File       This section demonstrates for writing log records to a log file. Logger provides different types of level like: warning, info
how to read this xml file - XML
how to read this xml file  i want to read this xml file using java... read i have tried lot more , but i am not able to read this xml file...[])throws Exception { File f=new File("C:/data.xml"); DocumentBuilderFactory
Run XML file
Run XML file  Hi.. How do I execute or run an XML file? please tell me about that Thanks
xml file creation in java
xml file creation in java  how to create xml file in java so that input should not be given from keyboard. and that file should be stored.   Please visit the following links: http://www.roseindia.net/tutorial/java/xml
How to prepare XML file?
How to prepare XML file?  Hi, I want to prepare XML File, can you... you create an XML file.. Create XML File using Servlet How to generate build.xml file Complete Hibernate 3.0 and Hibernate 4 Tutorial Thanks
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
xml configuration file - JDBC
xml configuration file  Hi, Could you please tell me how to write a xml configuration file . We have mysql database in some other system. I have... to create a xml file. Please help me out. Thank you  Hi Friend, Try
retrieve in xml file in struts2
retrieve in xml file in struts2  i am using struts2 and trying... can i get back the value from the xml file in the ajax function and print... properties file using ajax. i now put the values from properties file to map
Writing a file using servlets - JSP-Servlet
Writing a file using servlets  I'm using a servlet to read an input... reading the data and creating a file in my space. but it is not writing..., but its not writing in the xyz.txt file. I think writing in a servlet should
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
problem of writing to a local file ( JApplet ) - Applet
problem of writing to a local file ( JApplet )  Dear All, I want to program a guestbook using java applets but now I have problem of writing to a file from JApplet. it is working without any problem if i run the program using
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static... the binary file from another program as follows: m_dis = new DataInputStream
questions from an xml file
questions from an xml file  i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... stumped when it comes to reading the file and writing to a new file. My code
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Advertisements
 

 

 

DMCA.com