
Builds XML documents from code, offering lightweight and fast processing with minimal validation. Supports namespaces, processing instructions, DTDs, unsafe text, and customizable print options for XML rendering.
This library can be used to build xml documents from Kotlin code. It is based on the HTML builder described in the Kotlin docs
It is designed to be lightweight and fast. There isn't any validation except to escape text to not violate xml standards.
Apache 2.0
To use in Gradle, simply add the Maven Central repository and then add the following dependency.
repositories {
mavenCentral()
}
dependencies {
compile("dev.zwander.xmlbuilder:[VERSION]")
}Similarly in Maven:
<dependencies>
<dependency>
<groupId>dev.zwander</groupId>
<artifactId>xmlbuilder</artifactId>
<version>[VERSION]</version>
</dependency>
</dependencies>val people = xml("people") {
xmlns = "http://example.com/people"
"person" {
attribute("id", 1)
"firstName" {
-"John"
}
"lastName" {
-"Doe"
}
"phone" {
-"555-555-5555"
}
}
}
val asString = people.toString()produces
<people xmlns="http://example.com/people">
<person id="1">
<firstName>
John
</firstName>
<lastName>
Doe
</lastName>
<phone>
555-555-5555
</phone>
</person>
</people>class Person(val id: Long, val firstName: String, val lastName: String, val phone: String)
val listOfPeople = listOf(
Person(1, "John", "Doe", "555-555-5555"),
Person(2, "Jane", "Doe", "555-555-6666")
)
val people = xml("people") {
xmlns = "http://example.com/people"
for (person in listOfPeople) {
"person" {
attribute("id", person.id)
"firstName" {
-person.firstName
}
"lastName" {
-person.lastName
}
"phone" {
-person.phone
}
}
}
}
val asString = people.toString()produces
<people xmlns="http://example.com/people">
<person id="1">
<firstName>
John
</firstName>
<lastName
>Doe
</lastName>
<phone>
555-555-5555
</phone>
</person>
<person id="2">
<firstName>
Jane
</firstName>
<lastName>
Doe
</lastName>
<phone>
555-555-6666
</phone>
</person>
</people><t:root xmlns:t="https://ns.org">
<t:element t:key="value"/>
</t:root>val ns = Namespace("t", "https://ns.org")
xml("root", ns) {
"element"(ns, Attribute("key", "value", ns))
}You can also use the Namespace("https://ns.org") constructor to create a Namespace object that represents the default xmlns.
namespaces property.You can add processing instructions to any element by using the processingInstruction method.
xml("root") {
processingInstruction("instruction")
}<root>
<?instruction?>
</root>Similarly you can add a global (top-level) instruction by call globalProcessingInstruction on the
root node. This method only applies to the root. If it is called on any other element, it will be ignored.
xml("root") {
globalProcessingInstruction("xml-stylesheet", "type" to "text/xsl", "href" to "style.xsl")
}<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<root/>You can specify a DTD (Document Type Declaration).
xml("root") {
doctype(systemId = "mydtd.dtd")
}<!DOCTYPE root SYSTEM "mydtd.dtd">
<root/>Complex DTDs are not supported.
You can use unsafe text for element and attribute values.
xml("root") {
unsafeText("<xml/>")
}produces
<root>
<xml/>
</root>xml("root") {
attribute("attr", unsafe("{"))
}produces
<root attr="&#123;"/>You can now control how your xml will look when rendered by passing the new PrintOptions class as an argument to toString.
pretty - This is the default and will produce the xml you see above.
singleLineTextElements - This will render single text element nodes on a single line if pretty is true
<root>
<element>value</element>
</root>as opposed to:
<root>
<element>
value
</element>
</root>useSelfClosingTags - Use <element/> instead of <element></element> for empty tags
useCharacterReference - Use character references instead of escaped characters. i.e. ' instead of '
Check out the excellent Ksoup library for multiplatform XML parsing.
This library can be used to build xml documents from Kotlin code. It is based on the HTML builder described in the Kotlin docs
It is designed to be lightweight and fast. There isn't any validation except to escape text to not violate xml standards.
Apache 2.0
To use in Gradle, simply add the Maven Central repository and then add the following dependency.
repositories {
mavenCentral()
}
dependencies {
compile("dev.zwander.xmlbuilder:[VERSION]")
}Similarly in Maven:
<dependencies>
<dependency>
<groupId>dev.zwander</groupId>
<artifactId>xmlbuilder</artifactId>
<version>[VERSION]</version>
</dependency>
</dependencies>val people = xml("people") {
xmlns = "http://example.com/people"
"person" {
attribute("id", 1)
"firstName" {
-"John"
}
"lastName" {
-"Doe"
}
"phone" {
-"555-555-5555"
}
}
}
val asString = people.toString()produces
<people xmlns="http://example.com/people">
<person id="1">
<firstName>
John
</firstName>
<lastName>
Doe
</lastName>
<phone>
555-555-5555
</phone>
</person>
</people>class Person(val id: Long, val firstName: String, val lastName: String, val phone: String)
val listOfPeople = listOf(
Person(1, "John", "Doe", "555-555-5555"),
Person(2, "Jane", "Doe", "555-555-6666")
)
val people = xml("people") {
xmlns = "http://example.com/people"
for (person in listOfPeople) {
"person" {
attribute("id", person.id)
"firstName" {
-person.firstName
}
"lastName" {
-person.lastName
}
"phone" {
-person.phone
}
}
}
}
val asString = people.toString()produces
<people xmlns="http://example.com/people">
<person id="1">
<firstName>
John
</firstName>
<lastName
>Doe
</lastName>
<phone>
555-555-5555
</phone>
</person>
<person id="2">
<firstName>
Jane
</firstName>
<lastName>
Doe
</lastName>
<phone>
555-555-6666
</phone>
</person>
</people><t:root xmlns:t="https://ns.org">
<t:element t:key="value"/>
</t:root>val ns = Namespace("t", "https://ns.org")
xml("root", ns) {
"element"(ns, Attribute("key", "value", ns))
}You can also use the Namespace("https://ns.org") constructor to create a Namespace object that represents the default xmlns.
namespaces property.You can add processing instructions to any element by using the processingInstruction method.
xml("root") {
processingInstruction("instruction")
}<root>
<?instruction?>
</root>Similarly you can add a global (top-level) instruction by call globalProcessingInstruction on the
root node. This method only applies to the root. If it is called on any other element, it will be ignored.
xml("root") {
globalProcessingInstruction("xml-stylesheet", "type" to "text/xsl", "href" to "style.xsl")
}<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<root/>You can specify a DTD (Document Type Declaration).
xml("root") {
doctype(systemId = "mydtd.dtd")
}<!DOCTYPE root SYSTEM "mydtd.dtd">
<root/>Complex DTDs are not supported.
You can use unsafe text for element and attribute values.
xml("root") {
unsafeText("<xml/>")
}produces
<root>
<xml/>
</root>xml("root") {
attribute("attr", unsafe("{"))
}produces
<root attr="&#123;"/>You can now control how your xml will look when rendered by passing the new PrintOptions class as an argument to toString.
pretty - This is the default and will produce the xml you see above.
singleLineTextElements - This will render single text element nodes on a single line if pretty is true
<root>
<element>value</element>
</root>as opposed to:
<root>
<element>
value
</element>
</root>useSelfClosingTags - Use <element/> instead of <element></element> for empty tags
useCharacterReference - Use character references instead of escaped characters. i.e. ' instead of '
Check out the excellent Ksoup library for multiplatform XML parsing.