3

I know how to get data from string-array. I have done like this.

<string-array name="values">
   <item>value1</item>
   <item>value2</item>
</string-array>

String[] values = this.getResources().getStringArray(R.array.values);

But I dont know whether the below type string-array is possible and confused to get values from that.

<string-array name="country_data">
        <item>
            <country>Afghanistan</country>
            <countryCode>93</countryCode>
            <iso2>AF</iso2>
            <iso3>AFG</iso3>
        </item>
    </string-array>

If it is possible please help me to get the values.

3 Answers 3

2

I dont know whether the below type string-array is possible

It is not.

If you want to have arbitrary XML, use res/xml/ for whatever XML structure you like. You can then use getResources().getXml() to get an XmlPullParser that you can use to parse that XML.

Or, wrap the contents of each of your <item> elements in CDATA, which should prevent the resource parser from messing with them. Then, each string you retrieve from the array would be plain XML, that you would need to parse.

Or, go with JSON in res/raw/ or assets/, using your favorite JSON parser (e.g., Gson).

Or, use SQLiteAssetHelper to package a database in your app that has this data pre-loaded.

Or, use R. Zagórski's solution, of having one <string-array> per object, with <item> elements for each field of the object.

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

1 Comment

I preferred to go with XmlPullParser.
0

xml :

<string-array name="_name">
        <item>n11</item>
        <item>n12</item>
        <item>n13</item>
        <item>n14</item>
        <item>n15</item>
        <item>n16</item>
    </string-array>

Retrieve at java class:

   String[] test;
test = getResources().getStringArray(R.array._name); 

Comments

-1

Of course it is possible.

In summary:

  1. Define POJO representing your model
  2. Create a function to import string array of arrays from resources into this POJO

Remember that you cannot use custom xml entries, they have to be <item>. And you need to know it's type upfront.

Comments

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.