XML to Swift Struct Generator
Codable structs with CodingKeys, ready for XMLCoder
XML Input
Swift Output
What the generator writes
Paste an XML sample and you get one Swift struct per element name, conforming to Codable. Each struct has a CodingKeys enum that maps camelCase property names back to the exact XML names, so item-list or x:meta still decode.
Foundation has no XML decoder of its own, so the structs are written for the open-source XMLCoder library. XMLCoder reads attributes and child elements by key, and an element's own text through the empty key, so <price currency="USD">24.99</price> becomes currency plus 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 XML into XML Input or Upload a file. Repeated elements must appear twice to become arrays. Sample loads an 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 renames the top-level type (empty uses the root element name). Codable adds the conformance and CodingKeys; turn it off for plain structs. Optional for missing fields makes fields that are absent in some records optional.
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 your sample: whole numbers become Int or Int64, decimals Double, true/false Bool. Values with a leading zero stay 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. It is parsed in your browser. Inputs over 256 KB are rejected.
Related tools
Other converters you might need: