Open Rufy86 opened 2 years ago
This should work, and is in our unit tests. Have you set decoder.shouldProcessNamespaces = true
? That causes the namespaces to be stripped.
Hello @Rufy86,
In your code, ns2
is the namespace, which is different from the property name. If you don't care about namespaces, then the easiest solution might be:
struct AirportList: Codable {
let body: String
}
// and to decode the XML...
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = true
let airportList = try decoder.decode(AirportList.self, from: data)
thank you very much for the answer but I've still problems this is a piece oaf my xml `<?xml version="1.0" encoding="utf-8" ?>
AAL
AES
AAR
@Rufy86 are you expecting the whole hierarchy of XML inside that String? Because if so, that's not how XMLCoder was designed. Each XML element or attribute is decoded into it's own codable key/value. So you'll need to model your Codable type to match the expectations in XML.
I'm try to decode an XML returned by an API. the result is an error:
Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "ns2:body", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "ns2:body", intValue: nil)], debugDescription: "No attribute or element found for key CodingKeys(stringValue: \"ns2:body\", intValue: nil) (\"ns2:body\").", underlyingError: nil))
this is the struct `struct AirportList:Codable { let ns2:String
}` I've changed the key in model class, with any tag I need but the result is the same.
this framework really works?