Skip to content

Adding all elements with same prefix to their own struct. #288

Open
@jubishop

Description

@jubishop

say I have this xml:

  <channel>
    <title>The Changelog: Software Development, Open Source</title>
    <itunes:summary>Software&apos;s best weekly news brief, deep technical interviews &amp; talk show.</itunes:summary>
    <itunes:author>Changelog Media</itunes:author>
 </channel>

which is a stripped down example from the real world of parsing Podcast RSS Feeds :)

It'd be great if I could decode that into something like:

struct Channel: Codable {
 let title:String
 
 struct iTunes: Codable {
   let summary: String
   let author: String
 }
}

I know I can store all three values in a single flat Channel structure given what's in the Readme, but is there any advanced way to cause this deeper structuring, putting the elements prefixed with iTunes: into the nested iTunes struct?

Right now my flattened version looks like this:

  struct Channel: Codable {
    let title: String
    let itunesSummary: String?
    let itunesAuthor: String?

    enum CodingKeys: String, CodingKey {
      case title
      case itunesSummary = "itunes:summary"
      case itunesAuthor = "itunes:author"
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions