XML to Swift
Paste XML and instantly get quicktype-style Swift Codable struct definitions with inferred types, arrays, and optional properties.
Example
Input XML:
<catalog>
<book id="bk101" inStock="true">
<title>XML Guide</title>
<price>44.95</price>
<pages>320</pages>
</book>
<book id="bk102" inStock="false">
<title>Midnight Rain</title>
<price>5.95</price>
</book>
</catalog>
Generated Swift:
struct Root: Codable {
let book: [Book]
}
struct Book: Codable {
let id: String
let inStock: Bool
let title: String
let price: Double
let pages: Int?
}How it works
XML is parsed with the browser's DOMParser, then walked to build a type model where repeated tags become arrays and missing tags become optionals. The model is emitted as Swift structs conforming to Codable, with CodingKeys added when a tag name differs from its camelCase Swift property.
Good to know
XML to Swift turns a pasted XML document into ready-to-use Swift Codable structs, inferring property types from the actual values it sees. It is aimed at iOS, macOS, and server-side Swift developers who receive XML from a legacy API, an RSS or sitemap feed, a SOAP endpoint, or a config file and want a typed model to decode it with XMLDecoder or a similar parser, without hand-writing every struct.
Reach for it whenever you have a representative XML sample but no schema or generated client. Because it works from data rather than a DTD or XSD, the quality of the output depends on how complete your sample is: include at least two of any repeated element (like two <book> entries) so the tool can correctly collapse them into an array and detect which fields are sometimes absent.
Reading the result is straightforward. A repeated tag becomes a Swift array; a tag that appears in some siblings but not others becomes an optional with a trailing ?; and XML attributes are emitted as ordinary properties alongside child elements. Type inference is conservative — a field is Int only if every observed value is an integer, widens to Double when integers and decimals mix, and otherwise falls back to String. When a tag name cannot be a clean Swift identifier, a CodingKeys enum is generated to map the camelCase property back to the original XML name.
One practical caveat: the generated types describe the structure of your sample, not a guarantee about the API. Fields that happen to be present in every sample row will be non-optional, so if production data can omit them your decode may fail — review optionality by hand and loosen types where the source is unreliable. Everything runs locally in your browser via the built-in DOMParser, so you can safely paste internal or sensitive payloads.
Frequently asked questions
How are optional properties decided?
When an element appears in some sibling occurrences but is missing from others (for example a
tag present in onebut not another), the corresponding Swift property is marked optional with a trailing ? so decoding never fails on missing data.How are XML attributes handled?
Attributes are treated as properties of the element's struct alongside child elements, with their value types inferred the same way (Int, Double, Bool, String). The xmlns namespace declarations are skipped.
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
Can I convert XML to Swift Codable structs without writing them by hand?
Yes. Paste a representative XML sample and the tool generates Swift structs conforming to Codable, inferring scalar types, arrays for repeated tags, and optionals for fields that are missing from some siblings.
Does Swift's Codable support XML out of the box?
No. Foundation's JSONDecoder handles JSON natively, but Swift has no built-in XMLDecoder. To decode XML into these generated structs you typically add a library such as XMLCoder, which provides an XMLDecoder that works with Codable types.
How does the tool decide between Int, Double, and String?
It checks every observed value for a field. If all values are whole numbers it picks Int, if integers and decimals are mixed it widens to Double, and anything else (including empty values) becomes String.
What happens to XML namespaces and prefixes?
Namespace declarations (xmlns attributes) are skipped during conversion. Prefixed element and attribute names are used as written, which may require a CodingKeys mapping or manual cleanup if the prefix is not a valid Swift identifier.
Why is my generated array element named with a singular type name?
When a tag repeats, the property becomes an array and the tool derives a singular struct name from the plural tag (for example trimming a trailing s or turning ies into y), so a books list yields a Book element type.
Will these structs work for both encoding and decoding?
The generated types conform to Codable, so they support both directions, but the model is shaped around decoding a sample. Round-trip encoding back to identical XML depends on your XML library's handling of attributes versus elements and element ordering.
Is there a size or rate limit on how much XML I can convert?
There is no enforced limit or sign-up; conversion happens entirely in the browser, so the practical ceiling is your device's memory and how large a document DOMParser can handle smoothly.
Related tools