XMLCoder icon indicating copy to clipboard operation
XMLCoder copied to clipboard

This framework really works? keyNotFound

Open Rufy86 opened this issue 3 years ago • 4 comments

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

enum CodingKeys: String, CodingKey {
    case ns2 = "ns2:body"
}

}` I've changed the key in model class, with any tag I need but the result is the same.

this framework really works?

Rufy86 avatar Sep 05 '22 15:09 Rufy86

This should work, and is in our unit tests. Have you set decoder.shouldProcessNamespaces = true? That causes the namespaces to be stripped.

Joannis avatar Sep 05 '22 17:09 Joannis

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)

mflint avatar Sep 05 '22 17:09 mflint

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" ?> <ns2:body xmlns:ns2="http://www.jaapp.it"> <airportlist> <hash>F12887A97073B98718174A8AFCDE5F65</hash> <changed>true</changed> <airports> <airport> <id>AAL_APT_0001</id><code>AAL</code> <name>Int</name> <citycode>AAL</citycode> <city>Aalborg</city> <countrycode>DK</countrycode> <country>Danimarca</country> <continent>Europa</continent> <latitude>57,09275891</latitude> <longitude>9,849243164</longitude> </airport> <airport> <id>AES_APT_0019</id><code>AES</code> <name>Aalesund Vigra</name> <citycode>AES</citycode> <city>Aalesund</city> <countrycode>NO</countrycode> <country>Norvegia</country> <continent>Europa</continent> <latitude>62,5625</latitude> <longitude>6,119699955</longitude> </airport> <airport> <id>AAR_APT_0001</id><code>AAR</code> <name>Aarhus Airport</name> <citycode>AAR</citycode> <city>Aarhus</city> <countrycode>DK</countrycode> <country>Danimarca</country> <continent>Europa</continent> <latitude>56,2999992</latitude> <longitude>10,6190004</longitude> </airport> </airports> </airportlist> </ns2:body>

Now I've no error from decoder but the objects are empty

this is my code:

AF.request("url_to_my_api").responseData { response in guard let airportList = response.data else { return } let decoder = XMLDecoder() decoder.shouldProcessNamespaces = true let note = try! decoder.decode(AirportList.self, from: airportList) print(note) }

Rufy86 avatar Sep 07 '22 15:09 Rufy86

@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.

Joannis avatar Sep 13 '22 11:09 Joannis