CoreOffice / XMLCoder

Easy XML parsing using Codable protocols in Swift
https://coreoffice.github.io/XMLCoder/
MIT License
795 stars 107 forks source link

How to decode <itunes:episode> tags #201

Closed RichardAtDP closed 3 years ago

RichardAtDP commented 3 years ago

How would one go about decoding a tag with a colon in it, for example:

544 Thanks
MaxDesiatov commented 3 years ago

Hi @RichardAtDP, the colon in the name denotes the presence of the itunes namespace. You could strip it with the shouldProcessNamespaces property on XMLDecoder, as described in README.md. Does that answer your question?

RichardAtDP commented 3 years ago

Thanks @MaxDesiatov - I'm wondering how to integrate that option in a Combine pipeline?

MaxDesiatov commented 3 years ago

Did you check the existing section about Combine? The decoding operator takes a decoder instance as an argument, you can pass your own with shouldProcessNamespaces set as needed.

RichardAtDP commented 3 years ago

Got it! Thanks for your help.

For reference

let decoder = XMLDecoder();
decoder.shouldProcessNamespaces = true

self.cancellable = URLSession.shared.dataTaskPublisher(for: url)
            .map {  $0.data }
            .decode(type: Response.self, decoder: decoder )
            .catch { err in
                return Just( Response(info:info), error: "\(err)") )
            }
           .replaceError(with: Response(info:info), error: "\(err)") )
           .receive(on: DispatchQueue.main)
           .eraseToAnyPublisher()
           .assign(to: \.response, on: self)