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 BeginnersWriting 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 Beginnersjava 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
Writing and Reading A FileWriting 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 filewriting 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 Creating XMl file - XMLCreating
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 File Writing - Java BeginnersFile 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 - XMLxml 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 - XMLSorting
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. - XMLsplitting 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
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 create XML file - XMLHow 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
Run XML fileRun
XML file Hi..
How do I execute or run an
XML file?
please tell me about thatADS_TO_REPLACE_1
Thanks
xml file creation in javaxml 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 read this xml file - XMLhow 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
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
xml configuration file - JDBCxml 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
Writing a file using servlets - JSP-ServletWriting 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
problem of writing to a local file ( JApplet ) - Appletproblem 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
retrieve in xml file in struts2retrieve 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 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 filequestions 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 seach a keyword in a xml file - XMLhow to seach a keyword in a
xml file i have a
xml file. i wanted to read and store all the tag values. i have a html form in which when i enter some keyword say "a" then it has to display the tag values starting with a letter
write xml file with jsp useBean - JSP-Servletwrite
xml file with jsp useBean how to write into
xml files with jsp.. the code for
writing is in a class.. pls help me to solve this..thanx... an org.w3c.dom.Document from
XML. Save the
xml file at bin
file of C:\apache-tomat-5.5.23\bin
How to write to xml file in Java?How to write to
xml file in Java? Hi Friends,
Can anyone help me how to write in
xml file in Java programming. Please fill free to give example or reference website for getting example.
Hi,
To write in
xml file Reading a xml file - JSP-ServletReading a
xml file how to read a
xml file using jsp and then i have to retrive a data from that
file use it in code? Hi Friend,
Please visit the following link:
http://www.roseindia.net/jsp/parsing-xml.shtml
Reading a xml file - JSP-ServletReading a
xml file Thanks for ur answer sir but problem is that i have to do a reading a
xml file of a employee record and then i have to use a employee details to send mail to those employees how to do i sir please help me
how to modify content of XML file to
xml file to replace "XYZ".Every time with the content of text box the
file should...how to modify content of
XML file hi,
I want to modify content of a
xml file.There are two tags of similar name
say
<ContentName>C:\Myinfo
parsing XML file to get java object - XMLparsing
XML file to get java object Hello,
I'm facing a problem in parsing
XML file to get the java object.
I've tried to retrieve data from
XML file using SAX parser.
my
XML file structure is the following
how to convert text file to xml file in java. - XMLhow to convert text
file to
xml file in java. Hi all,
I m having some problem. Problem is I want to convert a text
file which is having the no of record(i.e no of different line of information)to a
xml file through java
SAX Parser for huge XML fileSAX Parser for huge
XML file Hi....
if the
XML file is small...("Enter
XML file name:");
String xmlFile = bf.readLine();
File file = new
File...();
}
}
}
..
What if the
xml file is very big or huge
file? and sometimes we wont
Java get XML File Java get
XML File
... the
XML file.
For this, you need to create a
XML file.
Here is the employee.xml
file:ADS_TO_REPLACE_1
<?
xml version="1.0"?>
<
xml file reading using javaxml file reading using java hi deepak
I want to read some data from
xml file and send that output to particular email address using java
import org.w3c.dom.*;
import org.w3c.dom.Node;
import javax.xml.parsers.
validation in struts2 using .xml filevalidation in struts2 using .
xml file how to do
xml validation in struts2 on dynamic fields
Hello Friend,
Please visit the following link:ADS_TO_REPLACE_1
http://www.roseindia.net/struts/struts2/struts2validation