Closed ptrkstr closed 5 years ago
Hi @patrickbdev, thanks for trying XMLCoder! This case is indeed supported, the only caveat is that you need to introduce an intermediate struct
to fully reflect the tree structure of your XML. If you don't provide custom coding keys and custom encoders/decoders, the names of your properties need to directly match the names of the elements and attributes (names of the types don't matter). In your example you don't have a property with name Child
, so you need to add that property to the intermediate struct
:
struct Response: Codable {
let Room: Int
let Children: Children
}
struct Children: Codable {
let Child: [Child]
}
struct Child: Codable {
let Name: String
let Age: Int
}
Does that resolve your issue?
@patrickbdev I worked around this caveat as follows: https://github.com/MaxDesiatov/XMLCoder/issues/10#issuecomment-509390151
That works perfectly, thanks @MaxDesiatov. I also discovered you had a CDCatalogue test that demonstrates this. Thank you for the time taken to reply. Also great idea @acecilia .
I'm loving XMLCoder but having a bit of difficulty understanding how to decode an array of elements. Here is an example XML
Here is how I'd expect it to be structured in Swift.
Here is how I'm decoding it:
The error printed is:
Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "Name", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "Children", intValue: nil), XMLKey(stringValue: "0", intValue: 0), XMLKey(stringValue: "0", intValue: 0), CodingKeys(stringValue: "Name", intValue: nil)], debugDescription: "No attribute or element found for key CodingKeys(stringValue: \"Name\", intValue: nil) (\"Name\").", underlyingError: nil))
Is this supported functionality of XMLCoder? I couldn't find any docs.