1

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.

tsiskoS
  • 31
  • 6
  • Not sure why the StreamResult class is failing to write to the file, but have you tried writing the results to a String, and then outputting the String to the file. As long as you have the correct permissions set, which you have, standard Java File I/O operations can be used in Android. – Sanj Apr 09 '15 at 14:33
  • are you sure your device SDK is lower than 18 ? because you have included this `android:maxSdkVersion="18"` into `android:name="android.permission.WRITE_EXTERNAL_STORAGE" ` permission – Mickey Tin Apr 09 '15 at 14:58

1 Answers1

1

Apparently the problem was with the manifest file. I tried to write the results to a string and then to a file as commented by Sanj (ty for the debug tip :P) and i got the permission denied error which i could not see earlier. Although i had included the write-external-permission, i found an answer to this post suggesting a different position of the use-permission tag.

So what did the trick was moving the

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

right before the end of the manifest file.

Community
  • 1
  • 1
tsiskoS
  • 31
  • 6