XMLParsing icon indicating copy to clipboard operation
XMLParsing copied to clipboard

Issue parsing XML with attributes and a string value

Open ciauri opened this issue 6 years ago • 3 comments

<SomeElement SomeAttribute="value">some string value</SomeElement> ends up only decoding as a string. I would expect to be able to have a struct like:

struct SomeElement: Codable {
    let value: String
    let attribute: String
    enum CodingKeys: String, CodingKey {
        case value = "#text"
        case attribute = "SomeAttribute"
    }
}

I receive the following error: "Expected to decode Array<Any> but found a string/data instead."

My xml is structured like this:

<ParentElement>
   <SomeElement SomeAttribute="value">some string value</SomeElement>
   <SomeElement SomeAttribute="value">some other string value</SomeElement>
</ParentElement>

And my struct is:

struct ParentElement: Codable {
   let someElements: [SomeElement]

   enum CodingKeys: String, CodingKey {
        case someElements = "SomeElement"
    }
}

struct SomeElement: Codable {
   let someAttribute: String
   let value: String

   enum CodingKeys: String, CodingKey {
        case someAttribute = "SomeAttribute"
        case value = "??"
    }
}

ciauri avatar Mar 15 '18 06:03 ciauri

Please, any solution to this?!

robiscjuan avatar Jan 03 '19 21:01 robiscjuan

Any solution for this ?

ThePredators avatar Oct 19 '21 09:10 ThePredators

This library is apparently no longer maintained. The issue is fixed in my XMLCoder fork with the coding key value intrinsic feature.

MaxDesiatov avatar Oct 19 '21 09:10 MaxDesiatov