问题描述
我正在尝试获取名称和阅读类型=alpha。
I'm trying to get the name and reading type="alpha".
我是初学者,英语不是我的第一语言,请原谅我。我已经阅读过有关DOM,SAX,Simple,其他StackOverflow帖子,其他示例但我不明白并且想要了解XMLPullParser的情况。
I'm a beginner and English is not my first language, please pardon me. I've read about DOM, SAX, Simple, other StackOverflow posts, other samples but I don't understand and will like to learn about XMLPullParser in this case.
以下示例XML:
<feed>
<title>Title</title>
<item>
<entry>
<name>Name1</name>
<record date="20001231">
<reading type="alpha" value="100"/>
<reading type="beta" value="200"/>
</record>
</entry>
<entry>
<name>Name2</name>
<record date="20001231">
<reading type="alpha" value="300"/>
<reading type="beta" value="400"/>
</record>
</entry>
</item>
</feed>
我读过这个:
和示例代码适用于上面的示例XML而没有< item>标记来获取名称和记录日期。
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
if (parser.getName().equals("entry")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
我尝试过使用< item>标签(但不起作用)是:
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
if (parser.getName().equals("item")) {
parser.next();
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
if (parser.getName().equals("entry")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
} else {
skip(parser);
}
}
return entries;
}
如果我能解决这个问题,我将能够阅读姓名和记录日期,但我想要得到的是名称和阅读类型=alpha,我不知道如何获得嵌套阅读类型= alpha。
If I can solve that, I will be able to read the name and record date, but what I'm trying to get is the name and reading type="alpha", which I don't know how to get the nested reading type="alpha".
非常感谢提前。
推荐答案
你继续循环,什么时候
parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("name")
您可以检索标记名称的值与 getText()
:
you can retrive the value of the tag name with getText()
:
您将调用
parser.next();
String name = parser.getText();
当
parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("reading")
你想读取你的属性,例如。 < reading type =alphavalue =300/>
you want to read your attributes, Eg. <reading type="alpha" value="300"/>
String type = parser.getAttributeValue(null, "type");
String value = parser.getAttributeValue(null, "value");
编辑:
private void readFeed(XmlPullParser parser) throws IOException, XmlPullParserException {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = parser.getName();
if (name == null) {
continue;
}
if (name.equals("reading")) {
Log.e(getClass().getSimpleName()," " + parser.getAttributeValue(null, "type"));
Log.e(getClass().getSimpleName(), " " + parser.getAttributeValue(null, "value"));
}
}
eventType = parser.next();
}
}
这篇关于在Android中使用XMLPullParser的XML嵌套标记很困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!