java - Parsing XML returns nothing -


I am currently trying to parse some XML using XmlResourceParser and then using ArrayAdapter on a ListView Showing information. The problem is, I did not get the result of ant.

My XML structure is like this:

  & lt; Resources & gt; & Lt; Categories & gt; & Lt; Animals & gt; & Lt; Words & gt; & Lt; English & gt; Animals & lt; / English & gt; & Lt; French & gt; Animals & lt; / French & gt; & Lt; Spanish & gt; Animals & lt; / Spanish & gt; & Lt; Portuguese & gt; Animals & lt; / Portuguese & gt; & Lt; / Words & gt; & Lt; / Animal & gt; & Lt; Transportation & gt; & Lt; / Transportation & gt; & Lt; Location & gt; & Lt; / Location & gt; & Lt; Clothes & gt; & Lt; / Clothes & gt; & Lt; Color & gt; & Lt; / Color & gt; & Lt; People & gt; & Lt; / People & gt; & Lt; Work & gt; & Lt; / Work & gt; & Lt; Society & gt; & Lt; / Society & gt; & Lt; Arts & gt; & Lt; / Art & gt; & Lt; Drinks & gt; & Lt; / Beverages & gt; & Lt; Food & gt; & Lt; / Food & gt; & Lt; Home & gt; & Lt; / Home & gt; & Lt; Electronics & gt; & Lt; / Electronics & gt; & Lt; Body & gt; & Lt; / Body & gt; & Lt; Nature & gt; & Lt; / Nature & gt; & Lt; Content & gt; & Lt; / Content & gt; & Lt; Math & gt; & Lt; / Math & gt; & Lt; Directions & gt; & Lt; / Directions & gt; & Lt; Weather & gt; & Lt; / Seasons & gt; & Lt; Number & gt; & Lt; / Number & gt; & Lt; Months & gt; & Lt; / Month & gt; & Lt; Day & gt; & Lt; / Day & gt; & Lt; Time & gt; & Lt; / Time & gt; & Lt; Actions & gt; & Lt; / Actions & gt; & Lt; Adjectives & gt; & Lt; / Adjectives & gt; & Lt; / Categories & gt;  

I am using the following code, to try and get all those categories:

  Public class MainActivity ActionBarActivity Expanded {Private ListView List; Private Array List & lt; String & gt; Array categories; @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_main); List = (ListView) findViewById (R.id.listView1); Try {array categories = parseCategories (); ArrayAdapter & LT; String & gt; ArrayAdapter = New Arrayheader & lt; String & gt; (This, R.DedlistView1, array categories); List.setAdapter (arrayAdapter); } Hold (IOException e) {e.printStackTrace (); }}  

and the parseCategories function is the following:

  Private ArrayList & lt; String & gt; ParseCategories () IOException {ArrayList & lt; String & gt; Categories = New Arrestists & lt; String & gt; (); 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 (); }} Hold (XmlPullParserException E) {Log.e ("XmlPullParserException", e.toString ()); } Parser.close (); Return categories; }  

When I come to parse XML, I am a little naive, can you guys help me with this one?

The problem is that the elements of the categories do not have "text"; However, there are many child elements.

The actual work can be summed up as:

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

This problem is slightly complex (another / or very easy) to be made, but it is possible. The move here is "leave" subtitles by placing the counter of a little depth; The recurring task will also work.

  int eventType = parser.getEventType (); While (eventType! = XmlPullParser.END_DOCUMENT) {if (eventType == XmlPullParser.START_TAG & amp; amp; parser.getName (.) Equals ("Categories")) // // catergories int begins in depth = 0; EventType = parser.next (); While {(Depth == 0 & amp; amp; amp; episode == XmlPullParser.END_TAG)) {// End categories if (eventType == XmlPullParser .START_TAG) {If (depth == 0) {// Category categories of direct hair .add (parser.getName ()); } Depth ++; } And if (eventType == XmlPullParser.END_TAG) {// Depth & gt; 0 depth -; } EventType = parser.next (); }} EventType = parser.next (); }  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -