Parse XML to NSDictionary

℡╲_俬逩灬. 提交于 2019-12-24 04:32:48

问题


I was using AFNetworking 1.4.3 to retrieve NSDictionary from JSON. Now I have to use new AFNetworking 2.0 to parse XML file. For example:

    [self getPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        // Mapping XML to my own MR_object
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // Error handling here
    }];

Is it possible to map XML file directly to NSDictionary without using NSXMLParser? I am looking for equvalent for this for in 2.0 and for XML input.


回答1:


yes you can avoid nsxmlparser. use libxml directly which is the C api that powers the NSXMLParser.

working code

Nice working code from apple:
https://developer.apple.com/library/ios/samplecode/XMLPerformance/Introduction/Intro.html
Not ONLY libxml but also show the usage of libxml

Tutorial

A nice article that explains how to use livxml:
http://www.cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html


idea 1

if you are looking for ready-made code then nicklockwood's XMLDictionary class looks like a very good fit:
https://github.com/nicklockwood/XMLDictionary
It Internally uses NSXMLParser though

idea 2

if you have a XSD File for your xml, check out my project xsd2cocoa:
https://github.com/Daij-Djan/xsd2cocoa
No NSXMLParser at all



来源:https://stackoverflow.com/questions/26553557/parse-xml-to-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!