XML to Swift Converter - Generate Swift Structs from XML Online
Convert XML data to Codable Swift structs that decode with XMLCoder.
XML to Swift converter, Swift structs, Swift code generation, XML parsing, XMLCoder, iOS development
How to Convert XML to Swift Structs - Step by Step Guide
Input Your XML Schema
Start by adding your XML data that needs to be converted to Swift structs. The tool analyzes the structure to generate appropriate Swift models with Codable protocol.
Example: Try This XML Data
Copy and paste this XML example to see how it works:
<?xml version="1.0" encoding="UTF-8"?> <ios-app id="APP-2024-001" version="2.1.0"> <app-info> <name>SwiftWeather</name> <bundle-id>com.example.swiftweather</bundle-id> <category>Weather</category> <min-ios-version>15.0</min-ios-version> <is-free>true</is-free> </app-info> <developer> <name>Apple Developer</name> <email>[email protected]</email> <verified>true</verified> </developer> </ios-app>
Check the Generated Structs
There are no settings to pick: every element with children or attributes becomes a Swift struct, and each one comes with the same pieces.
bundle-id can be read into bundleIdGet Generated Swift Code
Here is part of what the example above turns into. Attributes become properties too, and the comment after each property says where it came from.
Swift Code Output
Your XML becomes these Swift structs:
import Foundation import XMLCoder struct IosApp: Codable { let id: String? let version: String? let appInfo: AppInfo? let developer: Developer? enum CodingKeys: String, CodingKey { case id = "id" case version = "version" case appInfo = "app-info" } // MARK: - JSON Conversion func toJSON() -> Data? { return try? JSONEncoder().encode(self) } }
Copy or Download Swift Code
Paste the structs into your project, add XMLCoder, and load a document with Root.fromXML(data).
What is XML to Swift Conversion?
XML to Swift conversion transforms XML schemas into Swift structs with Codable protocol conformance for seamless XML and JSON serialization. This process analyzes XML structure and generates Swift models that can encode and decode XML data, making it easy to work with XML in iOS and macOS applications.
The generated Swift structs include proper type annotations, CodingKeys enums for custom property mapping, convenience methods for JSON/XML conversion, and integration with XMLCoder for native XML parsing in Swift applications.
Frequently Asked Questions
What does the generated Swift file contain?
One Codable struct for the root element and for every element that has child elements or attributes. Each struct has optional properties, a CodingKeys enum that maps the Swift names back to the XML names, an init() that sets everything to nil, and toJSON(), fromJSON(), toXML() and fromXML() helpers. Simple text elements such as <name> become properties on their parent instead of structs of their own.
Which packages do I need?
Foundation and XMLCoder (github.com/CoreOffice/XMLCoder), which you add through Swift Package Manager. The JSON helpers only need Foundation. Text inside an element is read through an empty coding key (case value = ""), which is how XMLCoder expects element text to be mapped.
How are the property types chosen?
From the text of every copy of an element, not just the first one. Whole numbers become Int, or Int64 when they don't fit in 32 bits, so an ID like 8901240544102066246 keeps all its digits. Decimals such as 14.50 become Double, only true and false become Bool, and anything else stays String. Values with leading zeros like 00501, hex like 0x1F, and elements that are sometimes empty stay String, because converting them would change or fail on the data.
How are repeated and nested elements handled?
Every copy of an element is merged, so a field that only shows up in the third <item> still makes it into the struct. An element that repeats under one parent becomes an array, nested elements get their own struct, and an element with both attributes and text keeps the attributes next to a value property. An element that can contain itself becomes an array, because Swift doesn't allow a struct to hold itself directly.
What happens to XML names that aren't valid Swift names?
Hyphens, dots and namespace prefixes are turned into camelCase (bundle-id becomes bundleId), names starting with a digit get a field prefix, Swift keywords such as class or default get a trailing underscore, and duplicates get a number. The CodingKeys enum still points at the original XML name, so decoding keeps working.
Does my XML get uploaded anywhere?
No. The conversion runs in your browser, so the XML you paste or upload stays on your machine. Very large files can make the page slow, since the browser does all the work.