Cannot access attributes
Hello,
First of all, I would like to thank you for KonsumeXML which is a really nice alternative to all those annotations based parsers! I'm trying to get rid of SimpleXML, its annotations and error messages (here is my project).
Sadly, I'm facing an issue when trying to access attributes. Here is my model, it is a classic RSS feed :
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Hacker News</title>
<atom:link href="https://news.ycombinator.com/feed/" />
<link>https://news.ycombinator.com/</link>
<description>Links for the intellectually curious, ranked by readers.</description>
</channel>
</rss>
Here is the code I'm using :
override fun fromXml(inputStream: InputStream): Feed {
val konsume = inputStream.konsumeXml()
val feed = Feed()
return try {
konsume.child("rss") {
child("channel") {
allChildrenAutoIgnore(names) {
with(feed) {
when (localName) {
"title" -> name = text()
"description" -> description = text()
"link" -> parseLink(this@allChildrenAutoIgnore, feed)
}
}
}
}
}
feed
} catch (e: Exception) {
throw ParseException(e.message)
}
}
private fun parseLink(konsume: Konsumer, feed: Feed) {
val url = konsume.attributes.getValueOpt("href")
if (url != null)
feed.url = url
else
feed.siteUrl = konsume.text()
}
companion object {
val names = Names.of("title", "description", "link")
}
As RSS feeds are usually a mess, I'm using allChildrenAutoIgnore
to get only the objects I need. But I'm getting a null value when I'm trying to access href
attribute from the atom:link
node. I can't really figure out if I did anything wrong.