Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue parsing XML file in key value structure #44

Open
zoufishanmehdi opened this issue Apr 12, 2019 · 1 comment
Open

Issue parsing XML file in key value structure #44

zoufishanmehdi opened this issue Apr 12, 2019 · 1 comment

Comments

@zoufishanmehdi
Copy link

zoufishanmehdi commented Apr 12, 2019

Hi, I'm trying to parse an XML file that has a key value structure but it doesn't look like it's supported. You can see the XML file using the link and the model can be seen below. How can I parse the file below using this library?

Link to XML file: http://mobile.public.ec2.nytimes.com.s3-website-us-east-1.amazonaws.com/candidates/content/v1/articles.plist

struct Articles: Codable {
    let article: [Dict]
    
    enum CodingKeys: String, CodingKey {
        case article = "dict"
    }
    
}

struct Dict: Codable {
    let title: String
    let body: String
    let images: [Images]
}

struct Images: Codable {
    let height: Int
    let url: String
    
}
@MaxDesiatov
Copy link

HI @zoufishanmehdi, you probably don't need XMLDecoder for this kind of format as a more high-level PropertyListDecoder would work. You also wouldn't need the Articles type in your snippet, Dict and Images are enough to decode it like this:

import Foundation

let result = try PropertyListDecoder().decode([Dict].self, from: simpleXML)

I hope this works for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants