Last active
February 13, 2018 08:41
-
-
Save Audhil/fc24d7055786330fba5e41f67cdb955c to your computer and use it in GitHub Desktop.
XML - Generation Blog_4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// XML generation by code | |
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) | |
} | |
// attribute | |
fun XmlSerializer.attribute(name: String, value: String) = | |
attribute("", name, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice!