在我的一个项目中,我正在努力解析来自原子xml的所有供稿,如下所示。
<entry>
<id>tag:blogger.com,1999:blog-4512083858574973357.post-3473864646179740942</id>
<published>2015-09-14T09:28:00.000+03:00</published>
<updated>2015-09-14T15:30:18.832+03:00</updated>
<title type="text">Mytitle</title>
<content>Hello</content>
<author>
<name>Author name</name>
<uri>URI HERE</uri>
<email>EMAIL HERE</email>
<gd:image rel="" width="32" height="32" src="" />
</author>
我可以使用以下代码获取所有标签,但我不知道如何解析作者的名称。
private Entry readEntry(XmlPullParser parser)
throws XmlPullParserException, IOException, ParseException {
parser.require(XmlPullParser.START_TAG, ns, "entry");
String id = null;
String title = null;
Uri image = null;
String link = null;
String content = null;
String description;
String author = null;
String shortDescription = null;
ArrayList tags= new ArrayList<>();;
long publishedOn = 0;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
switch (name) {
case "id":
id = readTag(parser, TAG_ID);
break;
case "title":
title = readTag(parser, TAG_TITLE);
break;
case "link":
link = readTag(parser, TAG_LINK);
break;
case "author":
//****************************************************
CANT GET THE NAME OF THE AUTHOR HERE
//****************************************************
break;
case "category": {
String tempLink = readTag(parser, TAGS);
if (tempLink != null) {
tags.add(tempLink);
}
break;
}
case "content": {
String tempLink = readTag(parser, TAG_CONTENT);
if (tempLink != null) {
content = tempLink;
image = Uri.parse(pullImageLink(content));
description = Html.fromHtml(content.replaceAll("<img.+?>", "")).toString().trim();
if (description.length() > 0) {
int descrLength = description.length();
if (descrLength > 250) {
shortDescription = description.substring(0, 250);
} else {
shortDescription = description.substring(0, descrLength);
}
}
}
break;
}
case "published":
publishedOn = getParsedDate(readTag(parser, TAG_PUBLISHED));
break;
default:
skip(parser);
break;
}
}
assert image != null;
return new Entry(id, title, image.toString(), link, content, shortDescription, publishedOn,tags, author);
}