Skip to content

A wrapper around org.w3c.dom with nicer Kotlin APIs ๐Ÿ˜˜

License

Notifications You must be signed in to change notification settings

martinbonnin/xoxo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

54 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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.4.0")

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

About

A wrapper around org.w3c.dom with nicer Kotlin APIs ๐Ÿ˜˜

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages