Reputation:
I am trying to parse a XML file in my android app and tried following the Android Developers examples on the site. However, even when all of the tags within the 'start' tags have been processed, the program continues in an infinite loop, never gets out of 'while (parser.next() != XmlPullParser.END_TAG)' loop even though my XML file starts and ends with <data>
and </data>
tags.
What am I doing wrong?
public List parse(InputStream in) throws XmlPullParserException, IOException { /* Function that takes Assay file as input stream */
try {
XmlPullParser parser = Xml.newPullParser(); /* Create new parser */
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); /* */
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser); /* extracts and processes the data */
} finally {
in.close();
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List girlNameList = new ArrayList();
List boyNameList = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "data");
while (parser.next() != XmlPullParser.END_TAG) {
parser.next();
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String currentTag = parser.getName();
if(currentTag.equals("emily")) {
girlNameList.add(currentTag);
} else if(currentTag.equals("robert") {
boyNameList.add(currentTag);
} else {
skip(parser);
}
}
return namesList;
}
<data>
<emily type="short" msg="======something=========="> </emily>
<emily type="tall" msg="======another message=========="> </emily>
<robert type="short" > </robert>
<emily type="tall" > </emily>
<robert type="tall" msg="use this later" > </robert>
<tester type="n/a" msg="should be skipped" > </tester>
</data>
Upvotes: 0
Views: 126
Reputation: 9784
Use
while (parser.next() != XmlPullParser.END_DOCUMENT)
You're skipping the last END_TAG in the condition check of the while loop.
Upvotes: 1