-1

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?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Cláudio Ribeiro
  • 1,509
  • 3
  • 37
  • 65
  • 1
    There is no "text" in the categories element, only more elements. (The only text is in the tags such as `Animal`, change `.equals("categories")` to `.equals("english")` to see.) – user2864740 Oct 17 '14 at 20:48
  • 1
    I this you should get help my already answer question. [Click here][1] [1]: http://stackoverflow.com/questions/26397647/how-to-make-rss-news-reader-application-in-android/26398110#26398110 – Danial Hussain Oct 17 '14 at 20:49
  • So basically you're saying that I should 'hard code' the Categories, but how would I know which category does a particular word belongs to? – Cláudio Ribeiro Oct 17 '14 at 21:01

1 Answers1

1

The problem is there is no "text" in the categories element; there are, however, many child elements.

The real task can be trivially summed up as:

How to get the names of all elements which are direct children of the categories element?

This problem is made a little bit complicated by XmlPullParser (using a DOM and/or XPath would be much easier), but it is doable. The trick here is to "skip" the subtrees by keeping a little depth counter; recursive functions would also work.

int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {

  if (eventType == XmlPullParser.START_TAG
      && parser.getName().equals("categories")) { // catergories start

    int depth = 0;
    eventType = parser.next();
    while (!(depth == 0 && eventType == XmlPullParser.END_TAG)) { // end categories

      if (eventType == XmlPullParser.START_TAG) {
        if (depth == 0) {  // direct child of categories
          categories.add(parser.getName());
        }
        depth++;
      } else if (eventType == XmlPullParser.END_TAG) { // depth > 0
        depth--;
      }

      eventType = parser.next();
    }

  }

  eventType = parser.next();
}
Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220