Reham
Reham

Reputation: 1986

Write the Xml document to a file to save Changes in Blackberry

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);

Upvotes: 0

Views: 527

Answers (1)

tonymontana
tonymontana

Reputation: 5823

In order to insert new node(s) you should use Node#insertBefore() instead of Node#appendChild(). Check documentation here.

Replace

Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText);
myDocument.appendChild(emp); 

with

Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText); 
myDocument.insertBefore(emp, someExistingNode); 

Where someExistingNode is the Node (probably Element) before which you want to add your new Node emp.

Edit 1: How to write XML to file

try {
    String filePath = "file:///store/home/user/XmlFile.xml";
    FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();  // create the file if it doesn't exist
    } else {
        fc.truncate(0); // truncate the file if it exists
    }

    OutputStream os = fc.openOutputStream();
    XMLWriter xmlWriter = new XMLWriter(os);
    xmlWriter.setPrettyPrint();
    DOMInternalRepresentation.parse(myDocument, xmlWriter);
    os.close();
    fc.close();

} catch (Exception e) {
    // Place exception handling code here
}

Edit 2: Added code sample for node insertion and XML-to-file writing

try {
    // Creating document
    Document myDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    Element parentElement = myDocument.createElement("parentTag");

    // create first element and append it to parent
    Element firstElement = myDocument.createElement("firstElement");
    firstElement.appendChild(myDocument.createTextNode("1"));
    parentElement.appendChild(firstElement);

    // create third element and append it to parent
    Element thirdElement = myDocument.createElement("thirdElement");
    thirdElement.appendChild(myDocument.createTextNode("3"));
    parentElement.appendChild(thirdElement);

    // create second element and insert it between first and third elements
    Element secondElement = myDocument.createElement("secondElement");
    secondElement.appendChild(myDocument.createTextNode("2"));
    parentElement.insertBefore(secondElement, thirdElement);

    myDocument.appendChild(parentElement);

    // Writing document to file
    String filePath = "file:///store/home/user/XmlFile.xml";
    FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();  // create the file if it doesn't exist
    } else {
        fc.truncate(0); // truncate the file if it exists
    }

    OutputStream os = fc.openOutputStream();
    XMLWriter xmlWriter = new XMLWriter(os);
    xmlWriter.setPrettyPrint();
    DOMInternalRepresentation.parse(myDocument, xmlWriter);
    os.close();
    fc.close();            

} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
}     

Also check this question regarding XML creation on BlackBerry.

Upvotes: 1

Related Questions