Click to See Complete Forum and Search --> : Appending data to an xml file


daina
02-13-2006, 09:14 AM
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>


Can anyone plz help.Its urgent

Khalid Ali
02-13-2006, 12:44 PM
what is happening when you use the above code. You must provide us with any errors or your results..

daina
02-13-2006, 11:42 PM
Hi

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

Khalid Ali
02-14-2006, 07:00 AM
well that is the correct behaviour..lok at ur code, u do read the file in but then u over write it with the new document and its new elements evertime u run this code..u need to create the document from the file that u read in and then make chanes to it

daina
02-14-2006, 07:18 AM
Hi
thanks.It worked...

Khalid Ali
02-14-2006, 10:11 AM
glad to be of some help..:-)