Skip to content

Instantly share code, notes, and snippets.

@stephenjfox
Last active September 23, 2023 19:37
Show Gist options
  • Save stephenjfox/58770f7237741494f3a6aad07ce3284d to your computer and use it in GitHub Desktop.
Save stephenjfox/58770f7237741494f3a6aad07ce3284d to your computer and use it in GitHub Desktop.
Parse XML in Kotlin with Jackson
import com.fasterxml.jackson.annotation.JsonAlias
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonRootName
import java.util.*
data class LedgerActivityDetail(
@set:JsonProperty("TransactionType")
var loanType: String? = null,
@set:JsonProperty("Amount")
var amount: String? = null
)
@JsonRootName("LedgerActivityObject")
data class LedgerActivity(
@set:JsonProperty("LedgerTransactionDate")
var LedgerTransactionDate: String? = null,
@set:JsonProperty("Amount")
var amount: String? = null,
// HERE IS WHERE THE MAGIC HAPPENS!!!
@set:JsonAlias("LedgerTransactionDetails", "LedgerActivityDetailObject")
var LedgerActivityDetails: List<LedgerActivityDetail> = ArrayList()
)
@JsonRootName("ArrayOfLedgerActivityObject")
data class LedgerActivities(
@set:JsonProperty("LedgerActivityObject")
var ledgerActivities: List<LedgerActivity> = ArrayList()
)
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
internal val kotlinXmlMapper = XmlMapper(JacksonXmlModule().apply {
setDefaultUseWrapper(false)
}).registerKotlinModule()
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
internal inline fun <reified T : Any> parseAs(path: String): T {
val resource = /*acquire a classLoader*/.getResource(path)
return kotlinXmlMapper.readValue(resource)
}
fun main(args: Array<String>) {
val ledgersInCode = parseAs<LedgerActivities>("path to ugly.xml")
println(ledgersInCode.ledgerActivities[0]) // the first action!!!
}
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLedgerActivityObject xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<LedgerActivityObject>
<LedgerTransactionDate>7/6/2017</LedgerTransactionDate>
<Amount>1056.38</Amount>
<LedgerTransactionDetails>
<LedgerActivityDetailObject>
<Amount>1056.38</Amount>
<TransactionType>Simple</TransactionType>
</LedgerActivityDetailObject>
</LedgerTransactionDetails>
</LedgerActivityObject>
<LedgerActivityObject>
<LedgerTransactionDate>2/27/2017</LedgerTransactionDate>
<Amount>250.46</Amount>
<LedgerTransactionDetails>
<LedgerActivityDetailObject>
<TransactionType>Simple</TransactionType>
<Amount>250.46</Amount>
</LedgerActivityDetailObject>
</LedgerTransactionDetails>
</LedgerActivityObject>
<LedgerActivityObject>
<LedgerTransactionDate>1/31/2017</LedgerTransactionDate>
<Amount>124.28</Amount>
<LedgerTransactionDetails>
<LedgerActivityDetailObject>
<TransactionType>Simple</TransactionType>
<Amount>124.28</Amount>
</LedgerActivityDetailObject>
</LedgerTransactionDetails>
</LedgerActivityObject>
</ArrayOfLedgerActivityObject>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment