Swiftpack.co - Package - MaxDesiatov/XMLCoder


Encoder & Decoder for XML using Swift's Codable protocols.

CI Status Version License Platform Coverage

This package is a fork of the original ShawnMoore/XMLParsing with more options and tests added.


import XMLCoder

let xmlStr = """
    <body>Don't forget to use XMLCoder!</body>

struct Note: Codable {
    var to: String
    var from: String
    var heading: String
    var body: String

guard let data = xmlStr.data(using: .utf8) else { return }

let note = try? XMLDecoder().decode(Note.self, from: data)

let returnData = try? XMLEncoder().encode(note, withRootKey: "note")



  • Xcode 10
  • Swift 4.2
  • iOS 9.0 / watchOS 2.0 / tvOS 9.0 / macOS 10.10 or later


CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. You can install it with the following command:

$ gem install cocoapods

Navigate to the project directory and create Podfile with the following command:

$ pod install

Inside of your Podfile, specify the XMLCoder pod:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'YourApp' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks

  # Pods for Test
  pod 'XMLCoder', '~> 0.3.1'


Then, run the following command:

$ pod install

Open the the YourApp.xcworkspace file that was created. This should be the file you use everyday to create your app, instead of the YourApp.xcodeproj file.


Carthage is a dependency manager that builds your dependencies and provides you with binary frameworks.

Carthage can be installed with Homebrew using the following command:

$ brew update
$ brew install carthage

Inside of your Cartfile, add GitHub path to XMLCoder:

github "MaxDesiatov/XMLCoder" ~> 0.3.1

Then, run the following command to build the framework:

$ carthage update

Drag the built framework into your Xcode project.

Swift Package Manager

Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding XMLCoder as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/MaxDesiatov/XMLCoder.git", from: "0.3.1")


This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to xmlcoder@desiatov.com.

Coding Style

This project uses SwiftFormat to enforce formatting style. We encourage you to run SwiftFormat within a local clone of the repository in whatever way works best for you either manually or automatically via an Xcode extension, build phase or git pre-commit hook etc. Please check SwiftFormat documentation for more details.

SwiftFormat also runs within our Travis CI setup and a CI build can fail with incosistent formatting. We require CI builds to pass for any PR before merging.

Test Coverage

Our goal is to keep XMLCoder stable and to serialize any XML correctly according to XML 1.0 standard. All of this can be easily tested automatically and we're slowly improving test coverage of XMLCoder and don't expect it to decrease. PRs that decrease the test coverage have a much lower chance of being merged. If you add any new features, please make sure to add tests, likewise for changes and any refactoring in existing code.


Stars: 57
Help us keep the lights on


Used By

Total: 1


0.3.1 - Feb 6, 2019

A bugfix release that adds missing CFBundleVersion in generated framework's Info.plist (#72 reported by @stonedauwg).


0.3.0 - Jan 22, 2019

A maintenance release focused on fixing bugs, improving error reporting and overall internal architecture of the library. For this release we've started tracking test coverage and were able to increase it from 11.8% to 75.6%. 🎉 Thanks to @hodovani and @regexident for their work on improving test coverage in this release.


You can now set errorContextLength: UInt property on XMLDecoder instance, which will make it add a snippet of XML of at most this length from parser state when a parsing error occurs. This change was provided by @hodovani and can greatly help with attempts to parse invalid XML, where previously only a line and column number were reported.


NodeEncodingStrategies was renamed to NodeEncodingStrategy for consistency. NodeEncodingStrategies is still available as a deprecated typealias, which will be removed in future versions. Thanks to @regexident for cleaning this up and providing many more changes in this release that make XMLCoder better and easier to use.


0.2.1 - Nov 18, 2018

  • watchOS deployment target set to 2.0 for Carthage (@MaxDesiatov)

0.2.0 - Nov 18, 2018

  • Add watchOS 2.0 deployment target (@MaxDesiatov)

0.1.1 - Nov 18, 2018

  • Set iOS deployment target to 9.0 (@MaxDesiatov)