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

时间:2015-07-28 19:58:07

标签: android xml transformer

下面的代码运行没有任何错误,但我的资源文件夹中的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();
                }

我在清单文件中使用此权限:

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

1 个答案:

答案 0 :(得分:0)

Your APK is read only. You cannot modify anything, incl. <form> <label class="control-label" for="name">Full Name</label> <input type="text" class="form-control" name="name" id="name" maxlength="30"> </form> folder. If you need its content to be sort of modifiable template, you must copy it out of assets/ first.