如何在android中更新我的xml文件



下面的代码运行没有任何错误,但我的xml文件在资产文件夹没有改变。为什么?

有没有更好的方法?

              try {
                    InputStream is = getAssets().open("file.xml");

                    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                    Document doc = docBuilder.parse(is);
                    Node earth = doc.getFirstChild();
                    NamedNodeMap earthAttributes = earth.getAttributes();
                    Attr galaxy = doc.createAttribute("galaxy");
                    galaxy.setValue("milky way");
                    earthAttributes.setNamedItem(galaxy);
                    Node canada = doc.createElement("country");
                    canada.setTextContent("ca");
                    earth.appendChild(canada);
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    //Initialize StreamResult with File object to save to file
                    StreamResult result = new StreamResult(new StringWriter());
                    DOMSource source = new DOMSource(doc);
                    transformer.transform(source, result);
                    String xmlString = result.getWriter().toString();
                    dialog.setMessage(xmlString);
                    dialog.show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }

我在manifest文件中使用这个权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

您的APK是只读的。不能修改任何内容,包括"assets/"文件夹。如果你需要它的内容是可修改的模板,你必须先从assets/复制出来

相关内容

  • 没有找到相关文章

最新更新