如何在android中更改具有键值对属性的xml的/编辑节点值?

编程入门 行业动态 更新时间:2024-10-27 14:29:29
本文介绍了如何在android中更改具有键值对属性的xml的/编辑节点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何更改 xml 的/edit 节点值,该值在 android 中具有添加"属性和键值对?下面是我的 xml,我想通过 android java 更改/编辑 xml 中 ipaddress 的值,并将该新文件保存到我从中读取 xml 值的文本文件中.

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 xml via android java and save that new file to the text file from which i have read the value of the xml.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
  <add key="ip" value="http://192.168.2.56:777/root/Calculator.Add" />
  <add key="comport" value="COM9" />
</appSettings>
</configuration>  

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
  <add key="ip" value="new value of  ip/ edited value" />
  <add key="comport" value="COM9" />
</appSettings>
</configuration> 

我试过这种方法来保存 ip 的值,但没有运气

I tried this way to save the value for ip ,but no luck

       saveConfigSettingsBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String  newIpAddress=ipAddressEditText.getText().toString();


              value = new ArrayList<String>(10);
                 key = new ArrayList<String>(10);
                 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);
                  //nodes.setTextContent(newIpAddress);
                  nodes.setNodeValue(newIpAddress);

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

推荐答案

我解决了编辑属性的问题,我可以编辑和保存add"元素的属性值,下面是对我有用的代码:

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

这篇关于如何在android中更改具有键值对属性的xml的/编辑节点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 02:44:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1054522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   键值   属性   编辑   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!