I want to edit a node's value of an existing XML file from the SD card.
我想从SD卡中编辑现有XML文件的节点值。
For example if I want to edit RouteName
's value from Mountain Trip to Sea Trip.
例如,如果我想编辑RouteName的值,从登山到海上旅行。
<Trip>
<RouteID>12345</RouteID>
<RouteName>Mountain Trip</RouteName>
</Trip>
I try to use the following code but it doesn't effect to the XML file on the SD card.
我尝试使用以下代码,但它不会影响SD卡上的XML文件。
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(
new File(Environment.getExternalStorageDirectory()+"/Trip/"+tripID+".trip"));
Node nodes = doc.getElementsByTagName("RouteName").item(0);
// newname is String variable which retrives value from edittext
nodes.setNodeValue(newname);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(
new File(Environment.getExternalStorageDirectory()+"/Trip/"+tripID+".trip"));
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
Why doesn't the XML file on the SD card get updated?
为什么SD卡上的XML文件没有被更新?
1 个解决方案
#1
10
I finally got the answer from here, thanks ProfSmiles.
我终于从这里得到了答案,谢谢你。
The fix was a change from nodes.setNodeValue
to nodes.setTextContent
.
修复是对节点的更改。setNodeValue nodes.setTextContent。
try {
String filePath = Environment.getExternalStorageDirectory()+"/Trip/"+tripID+".trip";
File file = new File(filePath);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
// Change the content of node
Node nodes = doc.getElementsByTagName("RouteName").item(0);
// I changed the below line form nodes.setNodeValue to nodes.setTextContent
nodes.setTextContent(newname);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
#1
10
I finally got the answer from here, thanks ProfSmiles.
我终于从这里得到了答案,谢谢你。
The fix was a change from nodes.setNodeValue
to nodes.setTextContent
.
修复是对节点的更改。setNodeValue nodes.setTextContent。
try {
String filePath = Environment.getExternalStorageDirectory()+"/Trip/"+tripID+".trip";
File file = new File(filePath);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
// Change the content of node
Node nodes = doc.getElementsByTagName("RouteName").item(0);
// I changed the below line form nodes.setNodeValue to nodes.setTextContent
nodes.setTextContent(newname);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(file);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}