Android parsing XML multiple levels deep



I am trying to parse an XML-file in Android, following their example in which they get a feed from stackoverflow.


I understand how and what they do, but I can't see how I can adjust it easily to fit my needs. My XML files looks as follows:



<?xml version="1.0" encoding="UTF-8"?>
<data-list>
<entry>
<meta-data>...</meta-data>
<compound>
<name>Numeric</name>
<entries>
<entry>
<meta-data>
...
<meta name="unit">mmHg</meta>
</meta-data>
<compound>
<name>Compound-Basic-Nu-Observed-Value</name>
<entries>
<entry>
<meta-data>
<meta name="partition">2</meta>
<meta name="metric-id">18949</meta>
</meta-data>
<simple>
<name>0</name>
<type>float</type>
<value>128.000000</value>
</simple>
</entry>
<entry>
<meta-data>
<meta name="partition">2</meta>
<meta name="metric-id">18950</meta>
</meta-data>
<simple>
<name>1</name>
<type>float</type>
<value>89.000000</value>
</simple>
</entry>
<entry>
<meta-data>
<meta name="partition">2</meta>
<meta name="metric-id">18951</meta>
</meta-data>
<simple>
<name>2</name>
<type>float</type>
<value>102.000000</value>
</simple>
</entry>
</entries>
</compound>
</entry>
<entry>
<compound>
<name>Absolute-Time-Stamp</name>
<entries>
<entry>
<simple>
<name>century</name>
<type>intu8</type>
<value>20</value>
</simple>
</entry>
<entry>
<simple>
<name>year</name>
<type>intu8</type>
<value>15</value>
</simple>
</entry>
<entry>
<simple>
<name>month</name>
<type>intu8</type>
<value>4</value>
</simple>
</entry>
<entry>
<simple>
<name>day</name>
<type>intu8</type>
<value>4</value>
</simple>
</entry>
<entry>
<simple>
<name>hour</name>
<type>intu8</type>
<value>16</value>
</simple>
</entry>
<entry>
<simple>
<name>minute</name>
<type>intu8</type>
<value>54</value>
</simple>
</entry>
<entry>
<simple>
<name>second</name>
<type>intu8</type>
<value>13</value>
</simple>
</entry>
<entry>
<simple>
<name>sec_fractions</name>
<type>intu8</type>
<value>0</value>
</simple>
</entry>
</entries>
</compound>
</entry>
</entries>
</compound>
</entry>
...
</data-list>


For now, only the <simple>-tags are of interest to me. From that I easily extract the name, type and value. Unclear is however how I can get only these tags.


I've already set the reading of the xml to start at data-list and reading each <entry>, so if I have 3 entries I get 3 things printed. But readEntry() only goes one level deep.



private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
Log.e("XML", "readFeed");
List<Entry> entries = new ArrayList<Entry>();

parser.require(XmlPullParser.START_TAG, ns, "data-list");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.v("XMLNAME", name);

// Starts by looking for the entry tag
if (name.equals("entry")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
Log.e("XML", "done readFeed");
return entries;
}

private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "entry");
String title = null;
float value = 0.0f;
String link = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if(name.equals("simple")){ // compound does change the value of summary
value = 1.1f;
}else{
skip(parser);
}
}
return new Entry(title, value, link);
}


So is there an easy way to go through all levels in search for the <simple>-tags?


No comments:

Post a Comment