Xoxo is a simple wrapper around org.w3c.dom to parse XML using nice Kotlin APIs. No more NodeList
, .item()
, etc... just use .children
, .filterIsInstance<>()
and all the Okio and Kotlin APIs we all love ๐.
Xoxo is designed for dynamic parsing in small JVM-only projects like Kotlin scripts and does not pretend to cover the full XML specification nor XML editing.
@file:DependsOn("net.mbonnin.xoxo:xoxo:0.3")
File("index.html").toXmlDocument()
.root
.childElements
.first { it.name == "body" }
.childElements
.first { it.name == "div" }
.textContent
val xmlString = """
<root>
<items>
<item videoId="1" title="title1">
<duration>30:30</duration>
</item>
<item videoId="2" title="title2">
<duration>31:00</duration>
</item>
</items>
</root>
""".trimIndent()
xmlString.toXmlDocument()
.root
.walkElements()
.filter { it.attributes.containsKey("videoId") }
.forEach {
println(it.attributes["title"])
println("duration:" + it.childElements.single().textContent)
}