1

I have the following rss: https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml and I can read all the info with no problem but one tag: <media:content height="151" medium="image" url="https://someurl.jpg" width="151"/>

I need the url from this tag but I dont know how to get the info from within the tag. The rest of the info is between two tags so, there's no problem but this one is on the tag itself.

My parse function is this

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()

In the last else if I'm trying to get the url value but doing parser.getAttributeValue(0) gives me IndexOutOfBoundsException and if I do text.toString() like in the other else if, doesn't crush but returns null of course because there is nothing between two tags Any clues on how to read info from the tag itself??? Many thanks

1 Answer 1

1

Try reading the attributes in START_TAG clause as follows.

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")
        }
        :
    }
    :
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.