Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit a node's value of an existing XML file from a SD card and save it back?

Tags:

dom

android

xml

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?

like image 881
April Smith Avatar asked Jan 24 '12 02:01

April Smith


People also ask

How do I change the value of a node in XML?

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".

How do I modify the nodes and content in a document?

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.

Where is the text value of an element Node stored?

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.

Is it possible to modify the children of a read-only 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.


1 Answers

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();
}
like image 79
April Smith Avatar answered Sep 22 '22 23:09

April Smith