i'm developing an android application and I want to read and edit an xml file stored in the internal storage of the device (the file is transferred to the device via usb to a specific folder, not created by the application ) .I use the DOMxmlParser of Java for this purpose, but i encounter a problem when I want to save back the updated version of the xml file. I have tested my editing function in my PC (as a simple Java Application) and it works perfect, however when i use the exact same code in the android app but no changes happen to the xml file and i get no errors.I also want to make clear that the file IS accessible since i can print its data from inside the app. Please help
Here is some Code:
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tsiskos.training1" >
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/app_name"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tsiskos.training1.MyActivity" />
</activity>
<activity
android:name=".EditContact"
android:label="@string/title_activity_edit_contact" >
</activity>
</application>
XML Editing Function
public void editContact(String number,String oldName,String oldLimit,String newName,String newLimit) {
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new File(xmlPath));
NodeList numbers = xmlDocument.getElementsByTagName("number");
for (int i = 0; i < numbers.getLength(); i++) {
if (((Element) numbers.item(i)).getAttribute("id").equals(number)) {
Element currNumber = (Element) numbers.item(i);
NodeList childs = currNumber.getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
Node currElement = childs.item(j);
if (currElement.getNodeName().equals("contact")) {
NodeList currContact = currElement.getChildNodes();
for (int k = 0; k < currContact.getLength(); k++) {
Node currContactChild = currContact.item(k);
String currNode = currContactChild.getNodeName();
String currValue = currContactChild.getTextContent();
if (currNode.equals("name") && currValue.equals(oldName)) {
currContactChild.setTextContent(newName);
}
if (currNode.equals("limit") && currValue.equals(oldLimit)) {
currContactChild.setTextContent(newLimit);
}
}
}
}
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(new File(xmlPath));
transformer.transform(source, result);
return xmlDocument;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
What i know for sure is that the reading and parsing of the file is successful. Do i miss something important related to the Android Filing System? Thanks in advance.