user3535205
user3535205

Reputation: 21

How to get an array of string from an xml file saved on internal storage in android?

I have a very simple xml file, created after the runtime, it's very similar to the res/array.xml but it's saved in the internal storage. It's something like:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="group0_arr">
        <item>1,name,desc</item>
        <item>2,name,desc</item>
        <item>3,name,desc</item>
    </string-array>

    <string-array name="group1_arr">
        <item>100,name,desc</item>
        <item>101,name,desc</item>
        <item>102,name,desc</item>
    </string-array>

</resources>

In my activity to get an array of string from the res/array.xml I use the code:

String[] arrayOfString0 context.getResources().getStringArray(R.array.group0_arr);

But to get the file on internal storage I use:

File darf= new File(this.getFilesDir(), "other_array.xml");

So how I can get the same result starting from that?

Upvotes: 0

Views: 167

Answers (1)

fweigl
fweigl

Reputation: 22008

You won't be able to apply the same 'simple' solution as getResources() for XML files other than some predefined ones like strings.xml. When you use getResources() Android already does a lot of work for you. If you want to use your own, custom XML file you need to parse it. You can do that 'by hand' with an XmlPullParser or a DOM-Parser or you can use a library like SimpleXML. It certainly won't be as convenient as the pre-defined resources that you use when using getResources().

If you want to store complex data within your app, I'd strongly recommend not using XML but rather JSON in combination with Gson or even an ORM library like OrmLite.

Upvotes: 1

Related Questions