I’m trying to parse Xml in Blackberry. I copied the xml to the SD card. I tried this code and I succeeded. I tried to insert new tags (Nodes) to the xml and it works but they are added to the end of the file but I don’t know if it is the best way to do that, but how can I write the Xml document to the file to save the changes??
DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(conn.openInputStream());
InsertBlock(doc);
doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
node=new String();
element = new String();
for (int i=0;i<list.getLength();i++){
Node value=list.item(i).getChildNodes().item(0);
node=list.item(i).getNodeName();
element=value.getNodeValue();
}
And for inserting new Nodes :
Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText);
myDocument.appendChild(emp);
In order to insert new node(s) you should use
Node#insertBefore()instead ofNode#appendChild(). Check documentation here.Replace
with
Where someExistingNode is the
Node(probablyElement) before which you want to add your newNodeemp.Edit 1: How to write XML to file
Edit 2: Added code sample for node insertion and XML-to-file writing
Also check this question regarding XML creation on BlackBerry.