'How to parse an XML element that has an attribute inside it ? Android ,Kotlin
I have an XML file from server. I use xmlPullPrser for parsing it into Kotlin, but it has an 'id' attribute . I don't Know how to get the 'id' value. thank you in advance for your answer. this is a part of my xml file :
```
<videoList >
<video id="843">
<title></title>
<author>Ghoneim</author>
</video>
<video id="887">
<title>Anatomic</title>
<author>Tewari</author>
</video>
</videoList>
```
this is some parts of my code for using XmlPullParser:
```
class VideoXmlParser {
@Throws(XmlPullParserException::class, IOException::class)
fun parse(inputStream: InputStream): List<Videos> {
inputStream.use { inputStream ->
val parser: XmlPullParser = Xml.newPullParser()
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false)
parser.setInput(inputStream, null)
parser.nextTag()
return readVideoList(parser)
}
}
}
@Throws(XmlPullParserException::class, IOException::class)
fun readVideoList(parser: XmlPullParser): List<Videos> {
val videos = mutableListOf<Videos>()
parser.require(XmlPullParser.START_TAG, ns, "videoList")
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.eventType != XmlPullParser.START_TAG) {
continue
}
// Starts by looking for the video tag
if (parser.name == "video") {
videos.add(readVideo(parser))
} else {
skip(parser)
}
}
return videos
}
```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
