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

Help with mix of attributes and elements #212

Closed
transat opened this issue Feb 10, 2021 · 1 comment
Closed

Help with mix of attributes and elements #212

transat opened this issue Feb 10, 2021 · 1 comment

Comments

@transat
Copy link

transat commented Feb 10, 2021

I'm having trouble decoding the following XML structure:

let sourceXML = """
<album>
    <details title="Helpless" artist="The Beatless" />
    <files>
        <file fileName="file A.wav" trackNum="1"></file>
        <file fileName="file B.wav" trackNum="2"></file>
        <file fileName="file C.wav" trackNum="3"></file>
    </files>
</album>
"""

struct Album: Codable {
    let details: Details
    let files: Files
    
    enum CodingKeys: String, CodingKey {
        case details
        case files
    }
    
    init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            details = try container.decode(Details.self, forKey: .details)
            files = try container.decode(Files.self, forKey: .files)
    }
}

struct Details: Codable, DynamicNodeDecoding, DynamicNodeEncoding{
    let title: String
    let artist: String
    
    enum CodingKeys: String, CodingKey {
        case title
        case artist
    }
    
    static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
        return .attribute
    }
    
    static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
        return .attribute
    }
}

struct Files: Codable {
    let files: [File]
    
    enum CodingKeys: String, CodingKey {
        case files
    }
}

struct File: Codable, DynamicNodeDecoding, DynamicNodeEncoding {
    let fileName: String
    let trackNum: Int
    
    enum CodingKeys: String, CodingKey {
        case fileName
        case trackNum
    }
    
    static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
        return .attribute
    }
    
    static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
        return .attribute
    }
}

let album = try! XMLDecoder().decode(Album.self, from: Data(sourceXML.utf8))

With the above code I can only read the top level parameters but not the array of files.
Any tips?

@transat
Copy link
Author

transat commented Feb 10, 2021

Ah. All good! Got it working.

@transat transat closed this as completed Feb 10, 2021
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

1 participant