Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
farheen
Posts: 6
Registered: ‎02-16-2010
My Device: 9700
My Carrier: Vodafone

XML Update

Hi, I am new in BB app developement. i made one xml in filesystem and now i want to update values of some tags on a button click event. how it is possible. Thanks Far........
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: XML Update

[ Edited ]

Welcome to the forums.  :smileyhappy:

 

For getting mortification on a Button Click, I recommend you use a ChangeListener.  If the API doesn't give you enough, there are numerous examples on the forum. 

 

In your change listener, you just need to find your XML, read it in, update the values, write it back out again.  FileExplorerDemo will help you with this.  Other posts talk about updating XML and writing it out, so have a search round. 

 

What more do you want to know?

Please use plain text.
New Contributor
farheen
Posts: 6
Registered: ‎02-16-2010
My Device: 9700
My Carrier: Vodafone

Re: XML Update

thanks for reply.

 

I have done everything like create file/write file/read file.but now i want to make the changes in the xml

 

i.e.

 

<APPLICATIONS>
    <APP>
        <APP_NAME>Expense</APP_NAME>
        <APP_TYPE>Android</APP_TYPE>
        <APP_ICON_OVER_URL>sdf</APP_ICON_OVER_URL>
        <APP_STATUS>RELEASED</APP_STATUS>
        <APP_ICON_URL>http:/asdfsae.png</APP_ICON_URL>
        <APP_URL>htsdfsb/</APP_URL>
        <APP_NEW></APP_NEW>
        <APP_DISPLAY_ORDER></APP_DISPLAY_ORDER>
        <PERMISSION></PERMISSION>
        <APP_DEVICE_MODEL_ID></APP_DEVICE_MODEL_ID>
        <APP_VERSION>1.0</APP_VERSION>
        <APP_DEVICE_ID></APP_DEVICE_ID>
        <COD_FILE_COUNT>-1</COD_FILE_COUNT>
        <APP_ROLE>Expense</APP_ROLE>
        <APP_BUILD_DATE>2009-08-12</APP_BUILD_DATE>
        <APP_DOMAIN>Finance</APP_DOMAIN>
    </APP
    <APP>
        <APP_NAME>Qusery</APP_NAME>
        <APP_TYPE>BROWSER</APP_TYPE>
        <APP_ICON_OVER_URL>sadfy.png</APP_ICON_OVER_URL>
        <APP_STATUS>RELEASED</APP_STATUS>
        <APP_ICON_URL>httasdfpng</APP_ICON_URL>
        <APP_URL>htsdfsryWeb</APP_URL>
        <APP_NEW></APP_NEW>
        <APP_DISPLAY_ORDER></APP_DISPLAY_ORDER>
        <PERMISSION></PERMISSION>
        <APP_DEVICE_MODEL_ID></APP_DEVICE_MODEL_ID>
        <APP_VERSION>1.0</APP_VERSION>
        <APP_DEVICE_ID></APP_DEVICE_ID>
        <COD_FILE_COUNT>-1</COD_FILE_COUNT>
        <APP_ROLE>Query</APP_ROLE>
        <APP_BUILD_DATE>2009-11-10</APP_BUILD_DATE>
        <APP_DOMAIN>Domain</APP_DOMAIN>
    </APP

 

in this xml i want to change 'Jobs' with 'Filled'.

 

i am doing

 

  FileConnection fcCheckFile = null;
                InputStream inputStream = null;
                fcCheckFile = (FileConnection)Connector.open(resources.getString(com.vvdl.mobile.core.VividoBBResource.EzCore_DeviceMemory)+resources.getString(com.vvdl.mobile.core.VividoBBResource.EzCore_StorageDir)+"/" +
                        resources.getString(com.vvdl.mobile.core.VividoBBResource.EzCore_AppsFile));
                
                inputStream = fcCheckFile.openInputStream();
                
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = docBuilder.parse (inputStream);
                doc.getDocumentElement ().normalize ();
                
                NodeList ListofApps = doc.getElementsByTagName("APP");
                
                for (int s = 0; s < ListofApps.getLength(); s++) {
                    Node firstPersonNode = ListofApps.item(s);
                    
                    if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element firstPersonElement1 = (Element) firstPersonNode;
                        NodeList firstNameList1 = firstPersonElement1.getElementsByTagName("APP_STATUS");
                        Element firstNameElement1 = (Element) firstNameList1.item(0);
                        NodeList textFNList1 = firstNameElement1.getChildNodes();
                        
                        if (textFNList1.item(0).getNodeValue().trim().equals("RELEASED")) {
                            textFNList1.item(0).setNodeValue("INSTALLED");
                            
                            /*DOMSource source = new DOMSource(doc);
                            File ss = new File("C:\\Documents and Settings\\user1\\workspace\\fdgds\\src\\ABC.xml");
                            StreamResult result = new StreamResult(ss);
                            xformer.transform(source, result);*/
                        }
                    }
                }

 

Its not saving value in the xml.

 

I want to change BOLD tag with INSTALLED value..

 

Thanks

Far.......

 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: XML Update

After you modify the DOM tree to be what you want (which your code seems to be doing, as far as I can tell), the last step of writing it to a file can be done as follows:

 

  1. Change the call to Connector.open() to include a second parameter of Connector.READ_WRITE
  2. After calling docBuilder.parse(), close inputStream
  3. Use fcCheckFile to open an output stream to the same file.
  4. Create an XMLWriter object (let's call it 'writer') using the output stream.
  5. Configure writer as desired.
  6. Call DOMInternalRepresentation.parse(doc, writer)
  7. Close the output stream and also fcCheckFile.

The file should now contain the new XML.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Contributor
farheen
Posts: 6
Registered: ‎02-16-2010
My Device: 9700
My Carrier: Vodafone

Re: XML Update

thanks for your reply. i tried with your approach but still didnt get success. My Code for now :

 

 

 // CHANGE THE STATUS OF APPLICATION AS INSTALLED
                
                FileConnection fcCheckFile = null;
                InputStream inputStream = null;
                fcCheckFile = (FileConnection)Connector.open(resources.getString(com.vvdl.mobile.core.VividoBBResource.EzCore_DeviceMemory)+resources.getString(com.vvdl.mobile.core.VividoBBResource.EzCore_StorageDir)+"/" +
                        resources.getString(com.vvdl.mobile.core.VividoBBResource.EzCore_AppsFile), Connector.READ_WRITE);
                
                inputStream = fcCheckFile.openInputStream();
                //OutputStream outputStream = fcCheckFile.openOutputStream();
                //DefaultHandler writer =  new OutputStreamWriter(outputStream);
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = docBuilder.parse (inputStream);
                //Document doc = DOMInternalRepresentation.parse(doc, writer);
                
                
               
                doc.getDocumentElement ().normalize ();
                
                NodeList ListofApps = doc.getElementsByTagName("APP");
                
                for (int iLoop = 0; iLoop < ListofApps.getLength(); iLoop++) {
                    
                    Node firstPersonNode = ListofApps.item(iLoop);
                    
                    
                    
                    if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element firstPersonElement1 = (Element) firstPersonNode;
                        NodeList firstNameList1 = firstPersonElement1.getElementsByTagName("APP_STATUS");
                        Element firstNameElement1 = (Element) firstNameList1.item(0);
                        NodeList textFNList1 = firstNameElement1.getChildNodes();
                        
                        //NamedNodeMap nmn = firstNameElement1.getAttributes();
                        //Node updateNode = nmn.getNamedItem(nmn.item(iLoop).toString());
                        //updateNode.setNodeValue("INSTALLED");
                        
                        
                         //Node oldNode = textFNList1.item(0);
                        
                        if (textFNList1.item(0).getNodeValue().trim().equals("RELEASED")) {
                            textFNList1.item(0).setNodeValue("INSTALLED");
                            textFNList1.item(0).setTextContent("INSTALLED");
                        
                            //Node newNode = textFNList1.item(0);
                            
                            //Node flag = doc.replaceChild(newNode, oldNode);
                            
                            //Dialog.alert(textFNList1.item(0).getNodeValue());
                            doc.renameNode(textFNList1.item(iLoop), null, "INSTALLED");
                            //Dialog.alert(n.getNodeValue());
                            /*DOMSource source = new DOMSource(doc);
                            File ss = new File("C:\\Documents and Settings\\user1\\workspace\\fdgds\\src\\ABC.xml");
                            StreamResult result = new StreamResult(ss);
                            xformer.transform(source, result);*/
                        }
                    }
                }
                
            
                ///////////////////////SANJAY
                if(fcCheckFile != null){
                    fcCheckFile.close();
                     inputStream.close();
                }
                ///////////////////////////////
               

Please use plain text.