-
Appending data to xml file
Hi
I have an xml file.I have to write all the details I enterd through UI.Suppose I have UserId & UserName. In the XML it should come like this.
<Userdetails>
<user>
<userid>1<userid>
<username>aaa</username>
</user>
<user>
<userid>2<userid>
<username>ssss</username>
</user>
<user>
<userid>3<userid>
<username>asdfd</username>
</user>
</Userdetails>
I wrote the code like this.But In the XML file.it is overwriting the details.
How to append the details in xml file.I am using JAXP.
<CODE>
public class DataBaseActivity {
private static String User_Details = "Userdetails";
private static String Main = "user";
private static String UserId = "userId";
private static String UserName = "userName";
public void create_details(String userid,String userName) {
System.out.println("UserId " + userid);
System.out.println("userName " + userName);
OutputStream outStream;
try {
outStream = new FileOutputStream("user.xml");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.newDocument();
Element rootElement = document.createElement(User_Details);
document.appendChild(rootElement);
Element childroot = document.createElement(Main);
rootElement.appendChild(childroot);
Element em = document.createElement(UserId);
em.appendChild(document.createTextNode(userid));
childroot.appendChild(em);
em = document.createElement(UserName);
em.appendChild(document.createTextNode(userName));
childroot.appendChild(em);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(outStream);
transformer.transform(source, result);
outStream.flush();
outStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
</CODE>
it is overwriting the exixting details.It is comming as like this.
<Userdetails>
<user>
<userid>1<userid>
<username>aaa</username>
</user>
</Userdetails>
it will overwrite the details as
<Userdetails>
<user>
<userid>2<userid>
<username>dfcdf</username>
</user>
</Userdetails>
Can u tell me what to do
-
the method create_details() creates a new xml document on each invokation with the given data. so it#s no wonder that you get a xml document which only contains the data you gave to it. to append multiple datas to your xml document, you have to reuse the document object.
make it like this (only a suggestion, I had no time to test or write proper code...):
Code:
public class myClass{
private Document myDoc;
private Element rootElement;
public void createDocument(){
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = docBuilderFactory.newDocumentBuilder();
myDoc = docBuilder.newDocument();
rootElement = document.createElement(User_Details);
document.appendChild(rootElement);
}
public void addUserData(String userid,String userName){
Element childroot = myDoc.createElement(Main);
rootElement.appendChild(childroot);
Element em = myDoc.createElement(UserId);
em.appendChild(myDoc.createTextNode(userid));
childroot.appendChild(em);
em = myDoc.createElement(UserName);
em.appendChild(myDoc.createTextNode(userName));
childroot.appendChild(em);
}
public Document getDocument(){
return myDoc;
}
public static void main(String [] argv){
myClass mine = new myClass();
mine.crrateDocument();
mine.addData("1", "user1");
mine.addData("2", "user2");
OutputStream outStream = new FileOutputStream("user.xml");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(mine.getDocument());
StreamResult result = new StreamResult(outStream);
transformer.transform(source, result);
outStream.flush();
outStream.close();
}
}
-
Hi
Thanks.I got it worked...
Similar Threads
-
Replies: 1
Last Post: 11-28-2005, 04:07 PM
-
Replies: 1
Last Post: 02-05-2003, 09:03 AM
-
By Tomer Cagan in forum ASP.NET
Replies: 1
Last Post: 07-24-2001, 09:01 AM
-
By KENHOW in forum VB Classic
Replies: 0
Last Post: 07-13-2001, 04:02 PM
-
By Leena Kumawat in forum XML
Replies: 0
Last Post: 06-19-2001, 07:56 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks