XML Question

Hi,

We are developing one application in which at present we are in need of transactions with the XML file.All these days we were only reading from the XML file and passing it on to the client after processing it in our EJB server program..But, the present requirement wants us to do, not only reading but also, adding/deleting/editing nodes to the xml file programmatically.I hv taken the module of adding nodes to the xml file.Following is the codec I hv given for adding the new node for the xml file...This perticular codec is not giving any error, after executing the code it says that the node is added successfully but, if I open the xml file again both programatically and manualy the added node is not show at all...Can anybody tell me what might be the problem with the following code and also tell if something hv been missed out in the same.I will be very much thankfull to you if U can tell me the exact steps to be followed to make the following code work properly;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

// Get the document handle to which the node to be added.

Document doc = db.parse(convertToFileURL(filename));

// Create the new element that is to be added.

Element elem=doc.createElement("G555007");

// Get the element under which the node to be added...Here code is

// the parent node.

NodeList nl = doc.getElementsByTagName(code);

// Append the created new node to the parent node got.

Node nod=nl.item(0).appendChild(doc.createTextNode("G555007"));

// Print the following line after appending the node.

System.out.println("Node appended.............");

-

Thanks and Regards,

Ram...

[1789 byte] By [ramc352a] at [2007-9-28 19:26:41]
# 1
you didn't append elem to the doc (use doc.appendChild(elem))
NineGravesa at 2007-7-12 18:00:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
This is add-on to prev post.When you say node.appendchild it adds to the in-memory document object and not to the physical file. So, on a "commit" method you would have to serialize the in-memory content to the actual data file.
elangovansa at 2007-7-12 18:00:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
Hi,Ok I got the I hv to serialize the in-memory content after adding node to the xml file.But, Can any body please tell me how to do the same....Thanks to allRam...
ramc352a at 2007-7-12 18:00:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Hi,

you can serialize the in-memory node by using org.apache.xml.serialize.XMLSerializer.

OutputFormat ofmt = new OutputFormat("xml", "UTF-8", true);

FileOutputStream fout = new FileOutputStream(filename);

XMLSerializer ser = new XMLSerializer(fout, ofmt);

ser.serialize(doc);

This can serialize your modified Document.

k_i_shora at 2007-7-12 18:00:27 > top of Java-index,Other Topics,Patterns & OO Design...