David Yang

Tips and posts for iOS developers from an iOS developer.

As I was working on personal project, I needed a tool to build an XML file. It happened to coincide with the release of @resultBuilder, available with Swift 5.4 and from Xcode 12.5.

Here’s how I’m using it.

Requirements

The tool is available on the Github project page for DYXML.

Right now, it supports Swift Package Manager, so you can install it by adding it as a dependency to your existing Swift project.

dependencies: [
    .package(url: "https://github.com/davidy4ng/DYXML.git", .upToNextMajor(from: "1.0.0"))
]

Generating your XML document

DYXML being based on Swift’s Result Builder, it will enable us to build our XML document with build blocks. If you’re familiar with SwiftUI, it will probably look quite similar.

You only need 2 kind of blocks:

  • node: represents an XML tag, takes 3 parameters:
    • name: the tag name
    • attributes: an array of (String, String) tuple, the first value being the attribute name and the second its value
    • value: a String value or one or many other node.
  • document: represents your XML document and will preprend its output with the <?xml version="1.0" encoding="UTF-8"?>

Note: You’re not required to use document at all. But it’s better if you’re trying to generate a full XML document.

Here’s an example.

import DYXML

let xml = document {
    node("gpx", attributes: [
        ("xmlns", "http://www.topografix.com/GPX/1/1"),
        ("creator", "byHand"),
        ("version", "1.1"),
        ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"),
        ("xsi:schemaLocation", "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd")
    ]) {
            node("wpt", attributes: [("lat", "39.921055008"), ("lon", "3.054223107")]) {
                node("ele", value: "12.863281")
                node("time", value: "2005-05-16T11:49:06Z")
                node("name", value: "Cala Sant Vicenç - Mallorca")
                node("sym", value: "City")
        }
    }
}
let output = xml.toString(withIndentation: 2)

Now, by printing the output, you will get following XML on the console.

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="byHand" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
  <wpt lat="39.921055008" lon="3.054223107">
    <ele>12.863281</ele>
    <time>2005-05-16T11:49:06Z</time>
    <name>Cala Sant Vicenç - Mallorca</name>
    <sym>City</sym>
  </wpt>
</gpx>

Going further

DYXML supports if, if...else, switch and for...in statements.

You can easily loop or use conditional statements to build the content of your XML document.

let names = ["Luke", "Leia", "Anakin", "Rey", "Vador"]
let xml = document {
    node("peoples") {
        for name in names {
            if name != "Vador" {
                node("name", value: name)
            }
        }
    }
}

Output:

<?xml version="1.0" encoding="UTF-8"?>
<peoples>
  <name>Luke</name>
  <name>Leia</name>
  <name>Anakin</name>
  <name>Rey</name>
</peoples>

Conclusion

DYXML is a simple tool written in Swift. Feel free to check the source code on Github. It’s also open to contribution.