Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XML generation with Kotlin extension functions
// XML generation by code
// based on https://www.schibsted.pl/blog/back-end/readable-xml-kotlin-extensions/
fun XmlSerializer.document(docName: String = "UTF-8",
xmlStringWriter: StringWriter = StringWriter(),
init: XmlSerializer.() -> Unit): String {
startDocument(docName, true)
xmlStringWriter.buffer.setLength(0) // refreshing string writer due to reuse
setOutput(xmlStringWriter)
init()
endDocument()
return xmlStringWriter.toString()
}
// element
fun XmlSerializer.element(name: String, init: XmlSerializer.() -> Unit) {
startTag("", name)
init()
endTag("", name)
}
// element with attribute & content
fun XmlSerializer.element(name: String,
content: String,
init: XmlSerializer.() -> Unit) {
startTag("", name)
init()
text(content)
endTag("", name)
}
// element with content
fun XmlSerializer.element(name: String, content: String) =
element(name) {
text(content)
}
fun XmlSerializer.attribute(name: String, value: String) =
attribute("", name, value)
class DashBoardActivity : AppCompatActivity() {
@Inject
lateinit var xmlSerializer: XmlSerializer // val xmlSerializer = Xml.newSerializer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
makingXML()
}
private fun makingXML() {
val USD: Currency = Currency.getInstance("USD")
val EUR: Currency = Currency.getInstance("EUR")
val persons = listOf(
Person(Name("John", "Doe"),
listOf(
Account("JP Morgan Chase & Co", "000-111-222-333", USD),
Account("Goldman Sachs", "000-999999999-222-444", EUR)
)
),
Person(Name("Jane", "Doe"),
listOf(
Account("JP Morgan Chase & Co", "000-777-222-333", USD),
Account("Goldman Sachs", "000-888888888-222-444", EUR)))
)
val thisIsXML = xmlSerializer.document {
element("persons") {
attribute("version", "1.1")
attribute("created", Calendar.getInstance().time.toString())
persons.forEach { (name, accounts) ->
element("person") {
element("name") {
element("first", name.first)
element("last", name.last)
}
element("accounts") {
accounts.forEach { (bank, number, currency) ->
element("account") {
element("bank", bank)
element("number", number)
element("currency", currency.currencyCode)
}
}
}
}
}
}
}
println(thisIsXML) // refer output1
thisIsXML = xmlSerializer.document {
element("Movies") {
element("row") {
attribute("no", "1")
element("FL", "6000000066015") {
attribute("val", "TicketId")
}
element("FL", "Dunkirk") {
attribute("val", "MovieName")
}
element("FL") {
attribute("val", "TimeLog")
element("row") {
attribute("no", "1")
element("FL", "23/01/2018") {
attribute("val", "logDate")
}
element("FL", "08:00") {
attribute("val", "startTime")
}
}
}
}
}
}
println(thisIsXML) // refer output2
}
}
data class Name(
val first: String,
val last: String
)
data class Account(
val bank: String,
val number: String,
val currency: Currency
)
data class Person(
val name: Name,
val accounts: List<Account>
)
<?xml version="1.0" encoding="UTF-8"?>
<persons version="1.1" created="Mon Jan 22 19:15:38 GMT+05:30 2018">
<person>
<name>
<first>John</first>
<last>Doe</last>
</name>
<accounts>
<account>
<bank>JP Morgan Chase &amp; Co</bank>
<number>000-111-222-333</number>
<currency>USD</currency>
</account>
<account>
<bank>Goldman Sachs</bank>
<number>000-999999999-222-444</number>
<currency>EUR</currency>
</account>
</accounts>
</person>
<person>
<name>
<first>Jane</first>
<last>Doe</last>
</name>
<accounts>
<account>
<bank>JP Morgan Chase &amp; Co</bank>
<number>000-777-222-333</number>
<currency>USD</currency>
</account>
<account>
<bank>Goldman Sachs</bank>
<number>000-888888888-222-444</number>
<currency>EUR</currency>
</account>
</accounts>
</person>
</persons>
<?xml version="1.0" encoding="UTF-8"?>
<Movies>
<row no="1">
<FL val="TicketId">6000000066015</FL>
<FL val="MovieName">Dunkirk</FL>
<FL val="TimeLog">
<row no="1">
<FL val="date">23/01/2018</FL>
<FL val="startTime">08:00</FL>
</row>
</FL>
</row>
</Movies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.