如何用 xmlcoder 解码 xml?

How to decode xml with xmlcoder?

我正在制作一个简单的 rss reader 作为我的第一个项目,但不知道如何解析它。数据 returns 没有问题,但无法像 json 响应那样解析它,我尝试 xmlcoder 使用可编码协议进行解码,但也没有用。我究竟做错了什么?这是我的结构吗?我对 xml.

有点迷茫

我的结构:

struct News: Codable {
    let version: String
    let channel: Channel

    enum CodingKeys: String, CodingKey {
        case version = "version"
        case channel = "channel"
    }
}

struct Channel: Codable {
    let title: String
    let link: String
    let channelDescription: String
    let generator: String
    let item: [Item]

    enum CodingKeys: String, CodingKey {
        case title = "title"
        case link = "link"
        case channelDescription = "description"
        case generator = "generator"
        case item = "item"
    }
}

struct Item: Codable {
    let title: String
    let link: String
    let itemDescription: String
    let enclosure: Enclosure
    let pubDate: String
    let guid: GUID
    let date: Date

    enum CodingKeys: String, CodingKey {
        case title = "title"
        case link = "link"
        case itemDescription = "description"
        case enclosure = "enclosure"
        case pubDate = "pubDate"
        case guid = "guid"
        case date = "date"
    }
}

struct Enclosure: Codable {
    let url: String
    let length: String?
    let type: TypeEnum

    enum CodingKeys: String, CodingKey {
        case url = "url"
        case length = "length"
        case type = "type"
    }
}

enum TypeEnum: String, Codable {
    case imageJPEG = "image/jpeg"
    case imageJpg = "image/jpg"
    case imagePNG = "image/png"
}

struct GUID: Codable {
    let isPermaLink: String
    let text: String

    enum CodingKeys: String, CodingKey {
        case isPermaLink = "isPermaLink"
        case text = "text"
    }
}

Url代码:

func getDataFrom(url: URL, completion: @escaping (_ data: Data?, _ error: Error?)->()) {
    let session = URLSession(configuration: .default)
    let download = session.dataTask(with: url) { data, response, error in
        completion(data, error)
    }
    download.resume()
}

func getData() {
    getDataFrom(url: URL(string: "http://www.rssmix.com/u/12512309/rss.xml")!) { data, error in
        guard let data = data else {return}
        let decoder = XMLDecoder()
        decoder.shouldProcessNamespaces = true
        do {
            let note = try decoder.decode(News.self, from: data)
            print(note)
        } catch {
            print(error)
        }
    }
}

样本Xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
   <channel>
      <title>News</title>
      <link>http://www.rssmix.com/</link>
      <description>This feed was created by mixing existing feeds from various sources.</description>
      <generator>RSSMix</generator>
      <item>
         <title>Animaniacs' 10 Best Educational Songs, Ranked</title>
         <link>https://www.cinemablend.com/television/2559082/animaniacs-best-educational-songs-ranked</link>
         <description>&lt;p&gt;The Animaniacs are back and more colorful than ever, so there's no better time to look back at the trio's best educational songs.&lt;/p&gt;</description>
         <enclosure url="https://img.cinemablend.com/quill/7/7/7/7/5/3/777753a254515e18c686dad2db5ad075b571d955.jpg" length="127057" type="image/jpg" />
         <pubDate>Mon, 23 Nov 2020 13:00:00 GMT</pubDate>
         <guid isPermaLink="false">https://www.cinemablend.com/television/2559082/animaniacs-best-educational-songs-ranked</guid>
         <dc:date>2020-11-23T13:00:00Z</dc:date>
      </item>

好吧,用这个

改变 case text = "text" 后终于解码了
struct GUID: Codable {
    let isPermaLink: String
    let text: String

    enum CodingKeys: String, CodingKey {
        case isPermaLink = "isPermaLink"
        case text = ""
    }
}

非常感谢@Larme