0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I also recommend going the JSON/GSON route. It is much nicer to be able to work with model objects that are parsed out by a library than it is to manually parse things out yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.