Skip to content Skip to sidebar Skip to footer

How To Change /edit Node Value Of Xml Which Have An Attribute With Key And Value Pair In Android?

how to change /edit node value of xml which have an attribute 'add' with key and value pair in android ? Below is my xml , i want to change/edit the value of ipaddress in the

Solution 1:

I solved the issue of editing the attribute and i can edit and save the attribute value of the "add" element, below is code which worked for me :

            saveConfigSettingsBtn.setOnClickListener(new OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String  newIpAddress=ipAddressEditText.getText().toString();            
                 ArrayList<String> mImageLink = new ArrayList<String>();
                 try {
                  File root = Environment.getExternalStorageDirectory();
                  File file = new File(root, "config/App_config.txt");
                  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                  Document doc = docBuilder.parse(file);                    
               // Change the content of node
                  Node nodes = doc.getElementsByTagName("add").item(0);

                  NodeList keyList = doc.getElementsByTagName("add");
                  Node Keynode = keyList.item(0);
                  Element fstElmnt = (Element) Keynode;  
                  fstElmnt.setAttribute("value", newIpAddress);//set the value of new edited ip here
                  String newNodeValue =   fstElmnt.getAttribute("value");
                  Log.d("newNodeValue", newNodeValue);                

                  Transformer transformer = TransformerFactory.newInstance().newTransformer();
                  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

                  // initialize StreamResult with File object to save to file
                  StreamResult result = new StreamResult(file);
                  DOMSource source = new DOMSource(doc);
                  transformer.transform(source, result);

                  Log.d("newip", newIpAddress);
                 } catch (Exception e) {
                  System.out.println("XML Parsing Excpetion = " + e);
              } 
        }
    });

Post a Comment for "How To Change /edit Node Value Of Xml Which Have An Attribute With Key And Value Pair In Android?"