Xoxo ?
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.
Installation
@file:DependsOn("net.mbonnin.xoxo:xoxo:0.3")
Read the text content of the first <div> tag in a HTML file
File("index.html").toXmlDocument()
.root
.childElements
.first { it.name == "body" }
.childElements
.first { it.name == "div" }
.textContent
Find all elements with a “videoId” attribute and display their duration
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)
}