Swiftpack.co -  adam-fowler/xml-coding as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Swift based XML Encoder/Decoder for Codable classes
.package(url: "https://github.com/adam-fowler/xml-coding.git", from: "0.4.1")

XML Encoder

Swift CI

This is a XML encoder and decoder that integrates with the Swift Codable system. It can create an XML tree from a Codable class or create Codable classes from an XML tree. The encoder uses its own XML classes which fairly closely follow the format of the XMLNode classes found in the macOS and Linux Foundation classes.

Using the classes

The basic method for saving a XML file from a Codable class or struct is as follows

let xml = try XMLEncoder().encode(codable)
let xmlString = xml.xmlString

And to create a Codable class from xml data

let xmlDocument = try XML.Document(data: data)
let codable = try XMLDecoder().decode(Codable.self, from: xmlDocument.rootElement) 

Reference documentation can be found here.

Encoding arrays and dictionaries

In XML the way collections are encoded can vary quite a bit. In a previous version of this library I tried to implement this inside the encoder/decoder. I have removed this code in favour of using codable property wrappers. These allow us to mark up member variables of a class with how we want them to serialize. The library comes with a series of properties wrappers to control collection serialization. There are two property wrappers @CustomCoding to be used with non-optional member variables and @CustomOptionalCoding to be used with optional member variables. Both property wrappers have a CustomCoder generic variable. This CustomCoder is a protocol which has two static functions decode and encode. These functions are used to provide custom encoding and decoding for an object. The library comes with CustomCoder classes for arrays and dictionaries.

By default an object like the following

struct Object {
    var array: [Int]

would serialize as follows


By using the ArrayCoder object and a ArrayCoderProperties object to define the element names along with the @CustomCoding property wrapper

struct Object {
    struct MyArrayCoderProperties: ArrayCoderProperties { static let member = "member" }
    @CustomCoding<ArrayCoder<MyArrayCoderProperties, Int>>
    var array: [Int]

it now serialize as follows


There is a typealias DefaultArrayCoder for the common element name "member" which reduces the verbosity of the code a little. With this the struct would look like this.

struct Object {
    @CustomCoding<DefaultArrayCoder> var array: [Int]

There are similar structures for defining Dictionary encoding and decoding. Except they have three possible variables, the name of each element node, the name of the key node and the name of the value node.


Stars: 4
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

2 weeks ago
  • Fix decoding of optional values as attributes

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API