drmohundro / SWXMLHash

Simple XML parsing in Swift
MIT License
1.41k stars 205 forks source link

Is there any simpler way to convert XMLIndexer to a regular Swift dictionary? #220

Closed MehmetBal closed 5 years ago

MehmetBal commented 5 years ago

Hi,

Thanks for this library. I am looking for a way to convert the result object of XMLIndexer to a Swift dictionary. Typed Swift dictionaries conforms to Codable protocol which makes creating Plist easy (Ex: PropertyListEncoder().encode(dict).write(to: fileURL)). Currently, I have XMLIndexerDeserializable object that I parse to then I convert that to dictionary manually and call the PropertylistEncoder.

Does SWXMLHash provide such functionality? Otherwise, what would you suggest doing?

Thanks

MehmetBal commented 5 years ago

https://stackoverflow.com/questions/57794158/any-easier-way-to-convert-xml-to-plist/57795992#57795992

drmohundro commented 5 years ago

Glad you were able to find something and that you dropped the link to the approach here! I wonder if this might be a way to handle #197 as well given that Swift already has some pretty good Codable/JSON (via Encodable and Decodable) support.

MehmetBal commented 5 years ago

@drmohundro I think so. Isn't the below what #197 is looking for:

        let jsonEncoder = JSONEncoder()   
        jsonEncoder.outputFormatting = .prettyPrinted
        do {
            let data = try jsonEncoder.encode(data)
            try data.write(to: outputJSONURL)
            print("wrote to output url successfully")
            NSWorkspace.shared.open(outputJSONURL)
        } catch {
            print("error occured  ---> \(error)")
        }