Sling Academy
Home/Kotlin/Reading and Writing XML Files in Kotlin

Reading and Writing XML Files in Kotlin

Last updated: November 30, 2024

XML (eXtensible Markup Language) is a common format for data exchange between systems. In this article, we'll explore how you can read and write XML files using Kotlin, providing clear examples to guide you.

Introduction to XML Handling in Kotlin

Handling XML in Kotlin can be efficiently done using the org.w3c.dom package in conjunction with the XML Parser from Java Standard Library. Alternatively, for a more Kotlin-centric approach, we can use libraries like Jackson or kotlinx.serialization, which offer more features and ease of use.

Setting up the Project

Let's start by setting up a basic Kotlin project. To manage the dependencies, we'll use Gradle.

plugins {
    kotlin("jvm") version "1.8.0"
}

dependencies {
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.0")
}

Reading XML Files

To read XML files, you can make use of the Java built-in DocumentBuilder:

import javax.xml.parsers.DocumentBuilderFactory
import java.io.File

fun readXmlFile(filePath: String) {
    val xmlFile = File(filePath)
    val documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
    val document = documentBuilder.parse(xmlFile)

    document.documentElement.normalize()
    println("Root Element: " + document.documentElement.nodeName)

    val nodeList = document.getElementsByTagName("yourElementName")
    for (i in 0 until nodeList.length) {
        val node = nodeList.item(i)
        if (node.nodeType == org.w3c.dom.Node.ELEMENT_NODE) {
            val elem = node as org.w3c.dom.Element
            println("Element Value: " + elem.textContent)
        }
    }
}

readXmlFile("example.xml")

Writing to XML Files

To write an XML file, we use TransformerFactory and involve DOM manipulation:

import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import org.w3c.dom.Document
import org.w3c.dom.Element
import javax.xml.parsers.DocumentBuilderFactory

fun writeXmlFile(filePath: String) {
    val documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
    val document = documentBuilder.newDocument()

    val rootElement: Element = document.createElement("data")
    document.appendChild(rootElement)

    val element = document.createElement("elementName")
    element.appendChild(document.createTextNode("Your Value"))
    rootElement.appendChild(element)

    val transformer = TransformerFactory.newInstance().newTransformer()
    transformer.setOutputProperty(OutputKeys.INDENT, "yes")
    transformer.setOutputProperty(OutputKeys.METHOD, "xml")
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")

    val source = DOMSource(document)
    val result = StreamResult(File(filePath))
    transformer.transform(source, result)

    println("XML File created successfully.")
}

writeXmlFile("output.xml")

Using Jackson for Simplicity

The Jackson library allows reading and writing XML with ease, especially when the XML structure is complex.

import com.fasterxml.jackson.dataformat.xml.XmlMapper

data class SampleData(val name: String, val value: String)

fun readXmlWithJackson(filePath: String) {
    val xmlMapper = XmlMapper()
    val data = xmlMapper.readValue(File(filePath), SampleData::class.java)
    println("Read Data: $data")
}

fun writeXmlWithJackson(data: SampleData, filePath: String) {
    val xmlMapper = XmlMapper()
    xmlMapper.writeValue(File(filePath), data)
    println("XML written successfully.")
}

val sampleData = SampleData("Sample", "Value")
writeXmlWithJackson(sampleData, "jackson_output.xml")

Conclusion

As we have seen, Kotlin provides robust options for reading and writing XML, whether by leveraging the existing Java XML libraries or enhancing your work with libraries like Jackson for simple serialization and deserialization tasks. By choosing the right approach, you can efficiently manage XML data in your Kotlin applications.

Next Article: Encrypting and Decrypting Files in Kotlin

Previous Article: Processing CSV Files Using Kotlin’s File API

Series: Kotlin - File & OS

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin