Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更新资源中的xml节点_Android_Xml_Updatexml - Fatal编程技术网

Android 更新资源中的xml节点

Android 更新资源中的xml节点,android,xml,updatexml,Android,Xml,Updatexml,我有一个android应用程序,我正在尝试更改节点值 下面我可以从assets文件夹中获取xml文件,并获得我想要的特定节点 InputStream in_s = getApplicationContext().getAssets().open("platform.xml"); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFact

我有一个android应用程序,我正在尝试更改节点值

下面我可以从assets文件夹中获取xml文件,并获得我想要的特定节点

InputStream in_s = getApplicationContext().getAssets().open("platform.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = (Document) docBuilder.parse(in_s);

Node path = doc.getElementsByTagName("path").item(0);
path.setNodeValue(txtPath.getText().toString());
但当谈到转变时,我坚持了下来

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer trans = transFactory.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult("platform.xml");
//there should be something to write to xml file in assets.. I just cant figure it out..
trans.transform(source, result);

无法写入/更新资产文件夹中的文件。您需要将xml文件从资产复制到SD卡,然后对其进行修改

将xml复制到SD卡:

String destFile = Environment.getExternalStorageDirectory().toString();
try {

        File f2 = new File(destFile);
        InputStream in = getAssets().open("file.xml");
        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException ex) {
        System.out
                .println(ex.getMessage() + " in the specified directory.");
        System.exit(0);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
清单中的权限:

String destFile = Environment.getExternalStorageDirectory().toString();
try {

        File f2 = new File(destFile);
        InputStream in = getAssets().open("file.xml");
        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException ex) {
        System.out
                .println(ex.getMessage() + " in the specified directory.");
        System.exit(0);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Android资源都是只读的。您不能动态更改或修改它。您只能读取资源,既不能更新资源,也不能写入资源

好的,但是android文件上有没有可以编辑xml文件的地方?你是说在运行时在app/apk内部?不,不可能。@MCanSener不,android文件中没有可更新/修改的位置。如果您想更新/修改任何资源,那么您需要将其复制到SD卡中,然后您可以对其进行更改。哦,另一个问题。如果我将xml文件复制到sd卡,当应用程序关闭时它会被删除吗?打开时会再次复制此xml吗?所以每次我们打开新的xml都会被复制,不是吗?将有太多的xml复制文件,不是吗?除非您将其删除或用户将其从卡中删除,否则xml文件将保留。将xml复制到SD卡取决于您。您可以根据需要重用旧的xml或复制新的xml。此外,还可以删除已经使用但不需要的xml。