XML to Swift Converter Online — Generate Struct

Paste any XML and generate Codable Swift structs ready for XMLCoder. Attributes get CodingKeys entries, and nested structures get their own structs — all in your browser.

What is an XML to Swift converter?

An XML to Swift converter takes a real XML document and emits Codable Swift struct definitions ready for XMLCoder, the most widely used Swift XML library. The OpenFormatter generator inspects every element and attribute, infers Swift types, and produces a complete struct hierarchy with CodingKeys mapping for renamed and attribute fields — saving the tedious step of writing them by hand for RSS feeds, plist, or SOAP responses on iOS and macOS.

XML differs from JSON in two ways your Swift model has to honour. First, XML has attributes on elements (<book id="bk101">) — Swift's built-in JSONDecoder cannot represent them. Use XMLCoder with DynamicNodeDecoding to mark specific keys as attributes. Second, XML has namespaces; the basic generator strips them, but you can re-add them through CodingKey raw values. Both aspects are handled by the sample below.

Sample XML and the Swift struct it generates

Input XML

<book id="bk101" lang="en">
  <title>Swift Programming</title>
  <year>2020</year>
  <price currency="USD">49.99</price>
</book>

Generated Swift

struct Book: Codable {
    let id: String      // XML attribute
    let lang: String    // XML attribute
    let title: String
    let year: Int
    let price: Price

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case lang = "lang"
    }
}

Notice id and lang are flagged as attributes — pair the generated CodingKeys with XMLCoder's DynamicNodeDecoding conformance to mark them as attribute nodes at decode time. The nested <price currency="USD">49.99</price> generates a Price struct with one attribute property and a value mapped to "" for the text body — XMLCoder's convention.

How to convert XML to Swift — 4 steps

  1. Paste your XML. An RSS feed, a SOAP response, an Apple plist — anything well-formed.
  2. Click Convert. The browser parses the XML and generates Codable Swift structs for the full element tree.
  3. Review attribute mapping. Confirm CodingKeys cover every attribute and renamed property; add a DynamicNodeDecoding extension if needed.
  4. Drop into your Xcode project. Copy the output, add XMLCoder via SwiftPM, and decode real XML with XMLDecoder().decode(Root.self, from: data).

Codable + XMLCoder

Generates Codable structs with CodingKeys mapping — drop in XMLCoder for full attribute and namespace support without writing a SAX delegate.

Attribute Awareness

XML attributes are flagged in CodingKeys so you know which keys to mark .attribute via DynamicNodeDecoding. The distinction is preserved exactly so round-tripping is lossless.

Client-Side Only

Your XML — including SOAP responses with credentials — is parsed in JavaScript on your machine. Verify in DevTools: zero network requests on Convert.

Common use cases

  • check_circleGenerate Swift structs for an RSS or Atom feed reader on iOS
  • check_circleBuild Codable models for a SOAP service response on macOS
  • check_circleParse Apple plist XML into typed Swift structs
  • check_circleGenerate Swift types from an XSD-described XML payload
  • check_circleCreate decoder models for legacy enterprise XML in iOS apps
  • check_circleBuild typed structs for podcast feed parsing
  • check_circleGenerate Swift models for OPDS book catalog XML
  • check_circleCreate structs for an iTunes XML playlist export

Why client-side conversion matters

SOAP responses and enterprise XML often contain customer PII, API tokens, internal endpoint names, and database identifiers. Pasting them into a server-hosted converter is a compliance violation in most regulated industries. OpenFormatter parses the XML and generates Swift code entirely in JavaScript on your device — no upload, no logs, no cookies. Open DevTools → Network and confirm no requests fire when you click Convert.

More XML tooling

Validate, format, or convert XML to other languages — every tool runs locally in your browser.

Frequently Asked Questions

Does it use XMLCoder for attribute mapping?

Yes — the generated structs are designed for XMLCoder (github.com/CoreOffice/XMLCoder), the most widely used Swift XML Codable library. CodingKeys map property names to source XML names; attributes are detected by the leading @ in the source. To mark a key as an attribute at decode time, conform CodingKey to XMLCoderKey via DynamicNodeDecoding and return .attribute for those keys.

What if I do not want a third-party dependency — can I use Apple's XMLParser?

Yes, but XMLParser is a SAX-style delegate API that does not consume Codable structs. You would write a delegate manually that produces these structs. The generated Codable structs still serve as the target type — only the decoding plumbing changes.

How are XML attributes mapped to Swift properties?

XML attributes (e.g. <book id="bk101">) become let properties with a CodingKeys case mapping the Swift name to the XML attribute name. With XMLCoder, override DynamicNodeDecoding on the parent struct and return .attribute for these keys; otherwise the decoder defaults to treating them as elements.

How are repeated elements like <tag> in <tags> represented?

Repeated child elements become an Array property ([TagType]) on the parent struct. XMLCoder collects each occurrence into the array. For wrapped lists you can model an inner Tags struct that contains the array, mirroring the XML hierarchy exactly.

What about XML namespaces?

XMLCoder accepts namespace-qualified element names through CodingKeys. Use the full ns:localname string as the raw value (case bookId = "ex:book"). The basic generator strips namespaces; rewrite the CodingKeys raw values to round-trip namespaced documents.

Can elements with both attributes and text content be modeled?

Yes. <price currency="USD">49.99</price> generates a Price struct with two properties — currency mapped via CodingKey and a value field mapped to "" (the XMLCoder convention for text content). Combined with DynamicNodeDecoding, the decoder splits attribute and text body correctly.

Is the XML I paste sent to your servers?

No. XML is parsed by the browser DOMParser and the Swift code is generated entirely in JavaScript on your machine. Open DevTools → Network and you will see no requests when you click Convert. Safe for SOAP responses and configuration files containing API keys or credentials.

How do I decode XML into the generated struct?

With XMLCoder: let book = try XMLDecoder().decode(Book.self, from: data) — the Codable conformance plus CodingKeys are populated automatically. Add XMLCoder to your Package.swift: .package(url: "https://github.com/CoreOffice/XMLCoder.git", from: "0.17.0").

XML to Swift Converter Online — Generate Struct