简体   繁体   English

将XML解析为NSDictionary

[英]Parse XML to NSDictionary

I was using AFNetworking 1.4.3 to retrieve NSDictionary from JSON. 我正在使用AFNetworking 1.4.3从JSON检索NSDictionary Now I have to use new AFNetworking 2.0 to parse XML file. 现在,我必须使用新的AFNetworking 2.0来解析XML文件。 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 ? 是否可以在不使用NSXMLParser情况下将XML文件直接映射到NSDictionary I am looking for equvalent for this for in 2.0 and for XML input. 我正在2.0和XML输入中寻找等效项。

yes you can avoid nsxmlparser. 是的,您可以避免使用nsxmlparser。 use libxml directly which is the C api that powers the NSXMLParser. 直接使用libxml,这是为NSXMLParser提供动力的C api。

working code 工作代码

Nice working code from apple: 苹果公司的不错的工作代码:
https://developer.apple.com/library/ios/samplecode/XMLPerformance/Introduction/Intro.html https://developer.apple.com/library/ios/samplecode/XMLPerformance/Introduction/Intro.html
Not ONLY libxml but also show the usage of libxml 不仅是libxml,而且还会显示libxml的用法

Tutorial 教程

A nice article that explains how to use livxml: 一篇不错的文章,介绍了如何使用livxml:
http://www.cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html http://www.cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html


idea 1 想法1

if you are looking for ready-made code then nicklockwood's XMLDictionary class looks like a very good fit: 如果您正在寻找现成的代码,那么尼克洛克伍德的XMLDictionary类看起来非常合适:
https://github.com/nicklockwood/XMLDictionary https://github.com/nicklockwood/XMLDictionary
It Internally uses NSXMLParser though 虽然它内部使用NSXMLParser

idea 2 想法2

if you have a XSD File for your xml, check out my project xsd2cocoa: 如果您的XML具有XSD文件,请查看我的项目xsd2cocoa:
https://github.com/Daij-Djan/xsd2cocoa https://github.com/Daij-Djan/xsd2cocoa
No NSXMLParser at all 根本没有NSXMLParser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM