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("org.redundent:kotlin-xml-builder:[VERSION]")
}
Similarly in Maven:
<dependencies>
<dependency>
<groupId>org.redundent</groupId>
<artifactId>kotlin-xml-builder</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>
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 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 '
You can also read xml documents using the parse
methods. They provide basic
xml parsing and will build a Node
element to build upon.
For more advanced consuming, check out konsume-xml. It includes many more features for consuming documents.
- Making
private fun parse(Document)
public.
Thanks to @rkklai for requesting this!
- Fixing issue where a text element that contains an empty string doesn't respect
useSelfClosingTags
.
Thanks to @d-wojciechowski for finding this!
- Added new PrintOptions to control the indent character(s) used.
indent
default is\t
.
Thanks to @vRallev for adding this!
POTENTIAL BREAKING CHANGES
- All node types override
equals
andhashCode
. This could change the behavior of putting nodes in a Set or Map.
Thanks to @cbartolome for requesting this!
- Added new PrintOptions to use character references instead of HTML names.
Thanks to @senecal-jjs and @sleddog for adding this!
- Updated README
BREAKING CHANGES
- The
kotlin-reflect
dependency has been removed from the transitive dependnecies. This module is only used for controlling element order using@XmlType
. If your project depends on that feature, you will need to havekotlin-reflect
on the runtime classpath.
Thanks to @mvysny for requesting this!
- Adding new global processing instructions.
Thanks to @rjaros for requsting this!
- Fixing single line text element rendering for processing instructures and CData elements.
Thanks to @jonathan-yan for fixing this!
- Added ability to specify the xml version. This affects both the prolog and text escaping.
Thanks to @ZR8C for finding this!
- Added ability to add processing instructions. Use the new
processingInstruction
method to do this.
Thanks to @endofhome for adding this! - Added ability to add comments. Use the new
comment
method to do this.
Thanks to @ejektaflex for requesting this!
- Added more robust PrintOptions class to allow for more control over how xml is structured.
Fixes issue #16 - Attribute values are now fully escaped properly.
Thanks to @pkulak for finding and fixing this!
BREAKING CHANGES
- Changed Element.render method signature to use kotlin.text.Appenable instead of kotlin.text.StringBuilder. Any custom element types created will need to be updated
- Fixed incorrect handling of CDATA elements.
prettyFormat
should not alter the CDATA content. - Fixed nested CData elements
Big thanks to @TWiStErRob for finding and fixing both of these!
- Switched to Kotlin's
Charsets
instead of usingjava.nio.charset.StandardCharsets
.StandardCharsets
is not available in some versions of Android.
Thanks to @little-fish for submitting and fixing this!
BREAKING CHANGES
- Moved
prettyFormat
to a parameter oftoString()
.prettyFormat
in the constructor or util method is no longer available. Fixes issue #7
- Fixes issue #6
- Upgrading Gradle to 4.9
- Upgrading Kotlin to 1.2.60
- Fix issue #4
- Tweak generation code to be more concise
- Add flag to code generation to allow for member functions instead of extension functions
BREAKING CHANGES
org.redundent.kotlin.xml.Node.name
has been renamed toorg.redundent.kotlin.xml.Node.nodeName
to avoid clashes with attributes calledname
.
- Adding DSL generator project to generate kotlin-xml-builder DSL from a schema file. See kotlin-xml-dsl-generator for details
- Added ability to parse an xml document into a builder object using the new
parse
method. - Upgrading Gradle and Kotlin versions.
- Added a
sitemap
method to allow for easy generation of sitemaps (which is what this project was created for in the first place). - Added
String.invoke
for elements allowing you to specify elements by just their name (see docs above) - Added some searching methods to search child nodes.
filter
,first
,firstOrNull
, andexists
. - Added some mutation methods to allow you to add/remove/replace nodes in an element.
- Added convenience method for elements with just a name and value.
element("name", "value")
- Removed
ns
method. Please usenamespace(...)
instead. - Upgraded Gradle version