【问题标题】:how to change /edit node value of xml which have an attribute with key and value pair in android?如何更改/编辑 xml 的节点值,它在 android 中有一个带有键和值对的属性?
【发布时间】:2013-10-21 09:15:21
【问题描述】:

如何更改/编辑 xml 的节点值,该节点值在 android 中具有“添加”属性和键值对? 下面是我的 xml,我想通过 android java 更改/编辑 xml 中 ipaddress 的值,并将该新文件保存到我从中读取 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 的值,但没有运气

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

【问题讨论】:

    标签: java android xml xml-parsing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2019-06-19
      • 2021-12-24
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      相关资源
      最近更新 更多