我有以下rss:https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml我可以毫无问题地阅读所有信息,只有一个标签:<media:content height="151" medium="image" url="https://someurl.jpg" width="151"/>
我需要这个标签的url,但我不知道如何从标签中获取信息。其余的信息在两个标签之间,所以没有问题,但这一个在标签上。
我的解析功能是这个
fun parse(inputStream: InputStream): List<RssItem> {
try {
val factory = XmlPullParserFactory.newInstance()
factory.isNamespaceAware = true
val parser = factory.newPullParser()
parser.setInput(inputStream, null)
var eventType = parser.eventType
var foundItem = false
while (eventType != XmlPullParser.END_DOCUMENT) {
val tagname = parser.name
when (eventType) {
XmlPullParser.START_TAG -> if (tagname.equals("item", ignoreCase = true)) {
foundItem = true
rssItem = RssItem()
}
XmlPullParser.TEXT -> text = parser.text
XmlPullParser.END_TAG -> if (tagname.equals("item", ignoreCase = true)) {
rssItem?.let { rssItems.add(it) }
foundItem = false
} else if (foundItem && tagname.equals("title", ignoreCase = true)) {
rssItem!!.title = text.toString()
} else if (foundItem && tagname.contains("guid", ignoreCase = true)) {
rssItem!!.link = text.toString()
} else if (foundItem && tagname.equals("description", ignoreCase = true)) {
rssItem!!.description = text.toString()
} else if (foundItem && tagname.contains("creator", ignoreCase = true)) {
rssItem!!.creator = text.toString()
} else if (foundItem && tagname.equals("pubDate", ignoreCase = true)) {
rssItem!!.pubDate = text.toString()
}else if (foundItem && tagname.contains("content", ignoreCase = true)) {
rssItem!!.image = parser.getAttributeValue(0)
}
}
eventType = parser.next()
在上一个else if
中,我试图获得url值,但执行parser.getAttributeValue(0)
会得到IndexOutOfBoundsException
,如果我像在另一个else if
中那样执行text.toString()
,则不会崩溃,但会返回null
,因为两个标签之间没有任何内容有关于如何从标签本身读取信息的线索吗???非常感谢
尝试读取START_TAG子句中的属性,如下所示。
while (eventType != XmlPullParser.END_DOCUMENT) {
val tagname = parser.name
when (eventType) {
XmlPullParser.START_TAG -> if (tagname.equals("item", ignoreCase = true)) {
foundItem = true
rssItem = RssItem()
} else if (foundItem && tagname.contains("content", ignoreCase = true)) {
//rssItem!!.image = parser.getAttributeValue(0)
rssItem!!.image = parser.getAttributeValue("", "url")
}
:
}
:
}