我有一个 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);