0

I have an android application which I am trying to change a node value.

Below I can take xml file from assets folder and get the specific node I want.

InputStream in_s = getApplicationContext().getAssets().open("platform.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = (Document) docBuilder.parse(in_s);

Node path = doc.getElementsByTagName("path").item(0);
path.setNodeValue(txtPath.getText().toString());

But when it comes to transform I stuck.

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer trans = transFactory.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult("platform.xml");
//there should be something to write to xml file in assets.. I just cant figure it out..
trans.transform(source, result);
MCanSener
  • 53
  • 9

2 Answers2

0

You can't write/update files in assets folder. You need to copy the xml file from assets to sdcard and then modify it.

Copy xml to sdcard:

String destFile = Environment.getExternalStorageDirectory().toString();
try {

        File f2 = new File(destFile);
        InputStream in = getAssets().open("file.xml");
        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException ex) {
        System.out
                .println(ex.getMessage() + " in the specified directory.");
        System.exit(0);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

Permission in manifest:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
fida1989
  • 3,234
  • 1
  • 27
  • 30
  • OK but is there any place on android files that we can edit our xml file? – MCanSener Feb 03 '14 at 08:03
  • You mean inside app/apk during runtime? No, not possible. – fida1989 Feb 03 '14 at 08:04
  • @MCanSener No there is not place in android files which you can update/modify. If you want to update/modify any resources then you need to copy that into the sdcard and then you can change it. – GrIsHu Feb 03 '14 at 08:04
  • Oh another question. If I copy the xml file to sd card will it be deleted when the app closed? and when opened will this xml copied again? So every time we open new xml will be copied isnt it? There will be too much xml copy files isnt it? – MCanSener Feb 03 '14 at 08:08
  • The xml file will remain unless you delete it or the user deletes it from card. Copying of xml to sdcard depends on you. You can reuse old xml or copy new one as needed. Also you can delete xml which are already used and not needed. – fida1989 Feb 03 '14 at 08:12
  • Hi again i am trying to copy file but Im getting file not found exception in OutputStream out = new FileOutputStream(f2); – MCanSener Feb 03 '14 at 10:59
  • You need add "write external storage" permission in manifest. Check my edit. – fida1989 Feb 03 '14 at 11:10
  • Well I figured out by asking around why this happened. If you didint give any space when building emulator then sdcard not exist :) well that was pretty obvious but it happens. thanks a lot for your considerations anyway helped a lot.. – MCanSener Feb 03 '14 at 13:44
0

Android resources are all read-only. You can not change or modify it dynamically. You only can read the resources neither you can update it nor you can write into it.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102