英文:
XML Parsing in Android {Java}
问题
以下是你要求的代码部分的翻译:
public List parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser(); /* 创建新的解析器 */
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); /* 设置命名空间处理特性 */
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser); /* 提取和处理数据 */
} 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>
英文:
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>
答案1
得分: 1
使用:
while (parser.next() != XmlPullParser.END_DOCUMENT)
在 while 循环的条件检查中,您跳过了最后一个 END_TAG。
英文:
Use
while (parser.next() != XmlPullParser.END_DOCUMENT)
You're skipping the last END_TAG in the condition check of the while loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论