如何在 android 中更新我的 xml 文件?
How can update my xml file in android?
下面的代码 运行 没有任何错误,但资产文件夹中的 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"/>
您的 APK 是只读的。你不能修改任何东西,包括。 assets/
个文件夹。如果您需要它的内容是某种可修改的模板,您必须先将其从 assets/
中复制出来。
下面的代码 运行 没有任何错误,但资产文件夹中的 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"/>
您的 APK 是只读的。你不能修改任何东西,包括。 assets/
个文件夹。如果您需要它的内容是某种可修改的模板,您必须先将其从 assets/
中复制出来。