CoreOffice / XMLCoder

Easy XML parsing using Codable protocols in Swift
https://coreoffice.github.io/XMLCoder/
MIT License
795 stars 107 forks source link

CDATA Decoding not working #168

Closed Kirow closed 4 years ago

Kirow commented 4 years ago
struct Container: Codable {
    let value: Int
    let data: String
}

let xmlString =
"""
<container>
   <value>42</value>
   <data><![CDATA[lorem ipsum]]></data>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
let result = try decoder.decode(Container.self, from: xmlData)

result.data decoded into "". Actually there is an error in XMLDecoder.swift:212 - Expected String but found null instead.

If we go deeper - KeyedBox.swift:27 return elements[""].first as? SimpleBox. It will return nil because key is "#CDATA" in this case.

I've made simple code change that fixed problem in this case, but have no idea what it can break later.

.patch ``` diff --git a/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift b/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift index 9930bfb..a3e8742 100644 --- a/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift +++ b/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift @@ -24,7 +24,7 @@ struct KeyedBox { } var value: SimpleBox? { - return elements[""].first as? SimpleBox + return elements.values.first as? SimpleBox } } diff --git a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift index 0615ca2..a3fcb50 100644 --- a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift +++ b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift @@ -23,6 +23,10 @@ struct KeyedStorage { var keys: [Key] { return buffer.map { $0.0 } } + + var values: [Value] { + return buffer.map { $0.1 } + } init(_ sequence: S) where S: Sequence, S.Element == (Key, Value) { buffer = Buffer() ```
MaxDesiatov commented 4 years ago

Thanks for reporting it @Kirow, the fix for it will be included in the next release.

kasperkronborg commented 4 years ago

Hi @MaxDesiatov,

For when is the next release scheduled? Waiting in anticipation for this.

Thanks for a great XML coder!

MaxDesiatov commented 4 years ago

Hey @kasperkronborg, this is now published in the 0.11 release and is available with SwiftPM, CocoPods and Carthage. Thanks for your feedback, I appreciate your support 🙂