I'm currently trying to parse some XML using XmlResourceParser and then show the information on a ListView using ArrayAdapter. The problem is, I don't get ant results.
My xml structure is like this:
<resources>
<categories>
<animal>
<word>
<english>Animal</english>
<french>Animal</french>
<spanish>Animal</spanish>
<portuguese>Animal</portuguese>
</word>
</animal>
<transportation></transportation>
<location></location>
<clothing></clothing>
<color></color>
<people></people>
<job></job>
<society></society>
<art></art>
<beverages></beverages>
<food></food>
<home></home>
<electronics></electronics>
<body></body>
<nature></nature>
<material></material>
<math></math>
<directions></directions>
<seasons></seasons>
<numbers></numbers>
<months></months>
<days></days>
<time></time>
<verbs></verbs>
<adjectives></adjectives>
</categories>
Now, each category will have different words on them but, at this moment I only want to get all the categories and show them in a ListView. To try and get all the categories I'm using the following code:
public class MainActivity extends ActionBarActivity {
private ListView list;
private ArrayList<String> arrayCategories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
try {
arrayCategories = parseCategories();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.id.listView1,
arrayCategories );
list.setAdapter(arrayAdapter);
} catch (IOException e) {
e.printStackTrace();
}
}
And the parseCategories function is the following:
private ArrayList<String> parseCategories() throws IOException {
ArrayList<String> categories = new ArrayList<String>();
XmlResourceParser parser = getResources().getXml(R.xml.database);
try {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("categories")) {
categories.add(parser.nextText());
}
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
Log.e("XmlPullParserException", e.toString());
}
parser.close();
return categories;
}
I'm still a little bit unexperienced when it comes to parse xml, can you guys help me with this one?