473,418 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

how to store data in xml file...

149 100+
hi,
im new to xml..i developed one application using jsp file..if i enter the values it should store in xml file...for storing using servlet file...the problem is if i enter new values it is not appending to the xml....can any one help me out...im sndng the servlet file...its urgent

package test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

public class XmlServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String username="";
String password="";
if(request.getParameter("username")!=null && request.getParameter("password")!=null)
{
username = request.getParameter("username").toString();
password = request.getParameter("password").toString();
}
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,username,password);
pw.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}
}
public void createXmlTree(Document doc,String userid,String password) throws Exception
{
//This method creates an element node
Element root = doc.createElement("User");
//adding a node after the last child node of the specified node.
doc.appendChild(root);
Element child1 = doc.createElement("UserId");
root.appendChild(child1);
Text text = doc.createTextNode(userid);
child1.appendChild(text);
Element element = doc.createElement("Password");
root.appendChild(element);
Text text1 = doc.createTextNode(password);
element.appendChild(text1);
//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("E:/xgprs/xmlgprs/loginXml.xml");
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();

}
}

Thanks in advance,
madhuri..
Jun 16 '10 #1
4 4642
Dheeraj Joshi
1,123 Expert 1GB
This may help you.

Regards
Dheeraj Joshi
Jun 16 '10 #2
jkmyoung
2,057 Expert 2GB
You create the tree, but don't store it in a variable or do anything with it:

new XmlServlet().createXmlTree(doc,username,password);
You have to add this result to your document.
Jun 16 '10 #3
madhuriks
149 100+
@dheerajjoshim
hi,
thanks for ur reply i searched that one in google....unable to know where to code it..
Jun 16 '10 #4
madhuriks
149 100+
@jkmyoung
hi,
thanks for ur reply can u tell me where to add the result..in my code...bcoz im new to xml...im getting confused...
Jun 16 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SunX | last post by:
Question from a newbie. How do you write out a data file of floating point numbers? file.write only takes strings. Many thanks.
2
by: Rastarizer | last post by:
I use VB .NET 2003 and I want to store into a binary field of a SqlServer 2003 table a File that I got on my hard disk. Does anyone know how can I do that ? I also use an OpenFileDialog...
2
by: jmensch | last post by:
Hello. I'm a reasonably new ASP.NET programmer with no prior ASP or web development experience, but a lot of general programming experience. I'm using Visual Web Developer Beta Express 2005. ...
36
by: Crirus | last post by:
Hi I have about 1000 records of users to store.. I dont like the ideea of a database server... How to store does data in a structured way? An XML? How big can became such an xml, because I...
3
by: Alex Shirley | last post by:
Hi Can you help me? I want to store a file of any description or format (from a hard disk) into memory in VB.NET (using ASP.NET). Then I want to use this datastructure in memory (containing...
9
by: gopi | last post by:
when installing db2, it only asks for installation directory and does not ask where it is going to store data files. how can i change the default directory after installation.
3
by: piotrek | last post by:
Hi I would like to ask you a question. Ian creating app. that download from server directory structure ( whole tree ) and those data are placed in proper places into my treeview control. I...
2
by: AB | last post by:
Hi to all, I have a problem about a importation of a file *.csv with SQL Server, through a bulk insert, called in a store procedure that a c# sw calls. This is the description of the error:...
2
by: phpnoob | last post by:
I have a php script that processes a form and then posts the user input to a data file on the server in a comma delimited format. For simplicity call the file "data.csv." The script is working...
2
by: creative1 | last post by:
Is this possible to store data in an XML file using HTML form? I got it working other way. I can store data in an xml file and display it in html form. I need to do it other way. It is very urgent. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.