0

Androidでキーと値のペアを持つ属性「追加」を持つxmlのノード値を変更/編集する方法は? 以下は私の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);
              } 
        }
    });
4

1 に答える 1

3

属性の編集の問題を解決し、「追加」要素の属性値を編集して保存できます。以下は私のために働いたコードです:

            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);
              } 
        }
    });
于 2013-10-21T10:14:54.350 に答える