XML to Swift Struct Generator
Codable structs with CodingKeys, ready for XMLCoder
XML Input
Swift Output
Paste your XML on the left and the Swift code shows up here.
Or drop a file on this box, , or .
What the generator writes
Hand the page an XML document and each element name becomes a Swift struct that adopts Codable. Every struct also gets a CodingKeys enum, and whenever the camelCase property differs from the XML name the case spells out the original, so item-list or x:meta still decode. A type that nests itself is written as a final class, because a struct cannot contain itself.
Foundation ships no XML decoder, so the output targets the open-source XMLCoder library. It reads attributes and child elements by key, and an element's own text through the empty key, so <rating scale="5">4.5</rating> gives a Rating struct with scale: Int, value: Double and case value = "". Repeated elements are arrays, and fields missing from some records are optionals.
A struct cannot contain itself, so a type that nests itself (categories in categories) is emitted as a final class. The output type-checks with Swift 6. The XML is parsed in your browser and never uploaded.
How to use it
Paste, pick two options, copy.
Paste a sample
Paste into XML Input or load a file through Upload. The first sample article holds two <tag> elements, and that repetition is what turns tag into [String]; a lone element stays a single value. Sample loads that article feed:
<feed updated="2026-03-02T08:00:00Z">
<article id="A-1001"><title>Shipping Flutter builds faster</title><tag>flutter</tag><tag>ci</tag></article>
<article id="A-1002" featured="true"><title>Offline sync</title><tag>dart</tag></article>
</feed> The output panel shows parse errors. The XML Validator gives the line and column.
Choose Codable and optionals
Root struct sets the outer type name; blank means <feed> becomes Feed. Untick Codable for bare structs with no conformance and no CodingKeys (a self-referencing class then gets a memberwise init). Optional for missing fields is the reason featured, present on only one article, comes out as Bool?.
Decode with XMLCoder
Copy or Download the .swift file, add XMLCoder with Swift Package Manager, then call try XMLDecoder().decode(Feed.self, from: data).
Where it helps
iOS apps on XML APIs
Older enterprise and government APIs still return XML. Codable structs keep the decoding code as short as it is for JSON.
RSS and podcast feeds
Paste one feed and get item structs with arrays for the repeated entries.
Config and plist-like files
macOS tools that read XML configs get typed values instead of string lookups.
Prototyping
Generate a model from a real response early, then refine names and types as the screen takes shape.
Common questions
Does this work with Foundation alone?
The structs compile with Foundation alone, but decoding XML needs a library. They are written for XMLCoder; Foundation's XMLParser is event-based and does not decode Codable types.
How are attributes handled?
As ordinary properties. XMLCoder decodes attributes and child elements by the same key lookup, so the CodingKeys entry is just the attribute name. If an attribute and a child element share a name, only the first one is kept.
Why is there a case value = ""?
That is how XMLCoder reaches an element's own text when the element also has attributes. See Apple's guide to CodingKeys for how custom keys work in general.
How are types inferred?
From what your sample holds. readMinutes values of 6 and 9 give Int, a number past the 32-bit range gives Int64, a decimal gives Double, and featured="true" gives Bool. Anything with letters or a leading zero, like an ID of 00419, is left as String.
Why is one type a final class?
It contains itself somewhere down the tree. Swift structs cannot do that, classes can.
Is my XML uploaded?
No. DOMParser reads it in your browser and the Swift text is put together in the same tab. Input is capped at 256 KB; a feed with two or three entries already covers every property.
Related tools
Other converters you might need: