Skip to content

Instantly share code, notes, and snippets.

@Bluexin

Bluexin/Main.kt Secret

Created August 27, 2024 21:19
plugins {
kotlin("jvm") version "2.0.20"
kotlin("plugin.serialization") version "2.0.0"
}
group = "local.test"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.1")
implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.1")
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import nl.adaptivity.xmlutil.serialization.XML
import nl.adaptivity.xmlutil.serialization.XmlElement
import nl.adaptivity.xmlutil.serialization.XmlNamespaceDeclSpec
import nl.adaptivity.xmlutil.xmlStreaming
fun main() {
val result = HelloWorld::class.java.classLoader.getResourceAsStream("test.xml")?.use {
XML {
defaultPolicy {
pedantic = false
isStrictAttributeNames = false
}
}.decodeFromReader(HelloWorld.serializer(), xmlStreaming.newReader(it.reader()))
} ?: error("Couldn't read file")
println(result)
}
@Serializable
@SerialName("test:HelloWorld")
@XmlNamespaceDeclSpec("test=https://test.local")
data class HelloWorld(
@XmlElement
val user: String
)
<test:HelloWorld xmlns:test="https://test.local">
<user>You!</user>
</test:HelloWorld>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment