This post demonstrates how to write XML files using DOM in Java. For reading XML files, please read this previously posted article – How to read XML in Kotlin using DOM Parser.
We are still using Kotlin 1.1.
Target XML Output
1 2 3 4 5 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> <person person-id="1001"> <last-name>Doe</last-name> <first-name>John</first-name> </person> |
Kotlin Codes
The key idea in building out elements or nodes in memory is to build them out like tree and branches (and even leaves).
[wp_ad_camp_1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.turreta.kotlin.xml.dom import org.w3c.dom.Document import org.w3c.dom.Element import java.io.File import javax.xml.parsers.DocumentBuilder import javax.xml.parsers.DocumentBuilderFactory import javax.xml.transform.OutputKeys import javax.xml.transform.Transformer import javax.xml.transform.TransformerFactory import javax.xml.transform.dom.DOMSource import javax.xml.transform.stream.StreamResult fun main(args: Array<String>) { val docBuilder: DocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder() val doc: Document = docBuilder.newDocument() // Create the root node val rootElement: Element = doc.createElement("person") // Set person-id attribute that represents a person id from the datasource rootElement.setAttribute("person-id", "1001") // Create an element for person's last name val lastName: Element = doc.createElement("last-name") // Append text to the lastName element (give that person a last name) lastName.appendChild(doc.createTextNode("Doe")) // Finally, append lastName to person element rootElement.appendChild(lastName) // Create an element for person's first name val firstName: Element = doc.createElement("first-name") // Append text to the firstName element (give that person a first name) firstName.appendChild(doc.createTextNode("John")) // Finally, append firstName to the person element right after the lastName element rootElement.appendChild(firstName) // Now, "add" the root node to the XML document in memory doc.appendChild(rootElement) val transformer: Transformer = TransformerFactory.newInstance().newTransformer() // ==== Start: Pretty print // https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java transformer.setOutputProperty(OutputKeys.INDENT, "yes") transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2") // ==== End: Pretty print transformer.transform(DOMSource(doc), StreamResult(File("C:\\xml-test-output.xml"))) } |
Single-line XML
These codes produce an XML file to elements properly indented for readability. At times, we want a single-line XML. For example,
1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?><person person-id="1001"><last-name>Doe</last-name><first-name>John</first-name></person> |
Simple comment out these codes from the above codes.
[wp_ad_camp_2]
1 2 | transformer.setOutputProperty(OutputKeys.INDENT, "yes") transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2") |