I want to edit a node's value of an existing XML file from the SD card.
For example if I want to edit RouteName
's value from Mountain Trip to Sea Trip.
<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.
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?
The nodeValue property is used to change a node value. The setAttribute() method is used to change an attribute value. The examples below use the XML file books.xml. Change an element's text node. This example uses the nodeValue property to change the text node of the first <title> element in "books.xml".
There are many ways you can modify the nodes and content in a document. You can: Change the value of nodes using the Value property. Modify an entire set of nodes by replacing the nodes with new nodes. This is done using the InnerXml property. Replace existing nodes with new nodes using the RemoveChild method.
The text value of an element node is stored in a child node. This node is called a text node. To change the text value of an element, you must change the value of the elements's text node.
It is valid to add or remove a read-only child from a node that can be edited. However, attempts to modify the read-only node itself throws an InvalidOperationException. An example of this is modifying the children of an XmlEntityReference node. The children are read-only and cannot be modified.
I finally got the answer from here, thanks ProfSmiles.
The fix was a change from nodes.setNodeValue
to 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With