关键方法:
NSDictionary *xmlContent = [NSDictionary dictionaryWithXMLString:dicItem[@"xmlContent"]];
NSString *xmlString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL];
为iOS和Mac OS平台上解析和生成XML提供了一个简单的方法,可方便地将XML文件转换为NSDictionary。可将任何字典的内容以XML形式输出。
首先当然是导入库。
我纠结了一天做的是将xml解析,转换成NSDictionary
下方举例:
{ "resultMsg": "获取成功", "resultCode": 1, "_rc": "success", "mpList": [ { "logo": "http://localhost.bi.cn/main/upload/201510/42091445914559867.jpg", "mpName": "第一父母", "xmlContent": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <xml><FromMpId>1</FromMpId><FromUserName><![CDATA[第一父母]]></FromUserName><CreateTime>2015-09-25 14:48:57</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><ArticleId><![CDATA[551]]></ArticleId><Title><![CDATA[EQ加油站 \\| 交换也是一种交往方式]]></Title><Description><![CDATA[当孩子用上百块的玩具车,换回了便宜的儿童画,家长该如何看待?是着眼于物品的经济价值,还是看到交换背后给孩子带来的成长?]]></Description><PicUrl><![CDATA[http://nmpic.xxt.cn/201509/19221442817544072.jpg]]></PicUrl><Url><![CDATA[http://nmmp.xxt.cn/appajax/viewArticle.do?articleId=551&s=youbeitong.cn]]></Url></item></Articles></xml>", "mpId": "1" }, { "logo": "http://nmmp.xxt.cn/mp/logo/2.png", "mpName": "校讯通助手", "xmlContent": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <xml><FromMpId>2</FromMpId><FromUserName><![CDATA[校讯通助手]]></FromUserName><CreateTime>2015-10-12 15:23:11</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><ArticleId><![CDATA[585]]></ArticleId><Title><![CDATA[家长课堂:家长常说10句话孩子更自信]]></Title><Description><![CDATA[自信,是你靠眼神传递给孩子的一种力量,是你能够为孩子添加的一双翅膀。要让孩子有接受挑战的勇气,不畏挫折的决心,关键就在于孩子的自信心。]]></Description><PicUrl><![CDATA[http://nmpic.xxt.cn/201510/37271444459901366.jpg]]></PicUrl><Url><![CDATA[http://nmmp.xxt.cn/appajax/viewArticle.do?articleId=585&s=youbeitong.cn]]></Url></item></Articles></xml>", "mpId": "2" }, ] }
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error]; NSInteger resultCode = [[dict objectForKey:@"resultCode"]integerValue]; if (resultCode==1){ NSMutableArray *items=[NSMutableArray array]; for (NSDictionary *dicItem in dict[@"mpList"]) { MainPageItem *item=[[MainPageItem alloc]init]; item.messageId=dicItem[@"mpId"]; item.messageImage=dicItem[@"logo"]; item.messageName=dicItem[@"mpName"]; //下方四句都是为获得item.messageContent服务的 NSDictionary *xmlContent = [NSDictionary dictionaryWithXMLString:dicItem[@"xmlContent"]]; NSDictionary *messageContentOne= xmlContent[@"Articles"]; NSDictionary *messageContentTwo = messageContentOne[@"item"]; item.messageContent = messageContentTwo[@"Description"]; [items addObject:item];
<strong><span style="font-size:18px;">就是因为我没有看返回的内容,所以直接</span></strong>xmlContent[@"<span style="font-family: Arial, Helvetica, sans-serif;">Description</span>"]; 导致错误好几次,下方正确的
NSDictionary *xmlContent = [NSDictionary dictionaryWithXMLString:dicItem[@"xmlContent"]]; NSDictionary *messageContentOne= xmlContent[@"Articles"]; NSDictionary *messageContentTwo = messageContentOne[@"item"]; item.messageContent = messageContentTwo[@"Description"];
这里是<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, STheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;">XML文件转换为NSDictionary的文件:以后认真的根据这个来写字典输出的内容:</span>
<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, STheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;"></span><pre name="code" class="objc">{ ArticleCount = 1; Articles = { item = { ArticleId = 586; Description = "\U201c\U54ce\U5440\Uff0c\U5b69\U5b50\U597d\U50cf\U611f\U5192\U4e86\Uff0c\U5feb\U62ff\U70b9\U6d88\U708e\U836f\U6765\U3002\U201d\U8fd9\U6837\U7684\U573a\U666f\U5728\U5f88\U591a\U5bb6\U5ead\U4e2d\U90fd\U4e0d\U964c\U751f\U3002\U53ef\U60a8\U6709\U6ca1\U6709\U60f3\U8fc7\Uff0c\U968f\U4fbf\U7ed9\U5b69\U5b50\U4f7f\U7528\U6297\U751f\U7d20\U5b58\U5728\U7684\U5371\U9669\U6027\U5230\U5e95\U6709\U591a\U5927\U5462\Uff1f"; PicUrl = null; Title = "\U6ee5\U7528\U6297\U751f\U7d20\U7684\U5371\U9669\U6027\Uff0c\U518d\U4e0d\U4e86\U89e3\U5c06\U4f1a\U4f24\U5230\U5b69\U5b50\Uff01"; Url = "http://nmmp.xxt.cn/appajax/viewArticle.do?articleId=586&s=youbeitong.cn"; }; }; CreateTime = "2015-10-10 16:34:17"; FromMpId = 6; FromUserName = "\U5065\U5eb7\U517b\U80b2"; MsgType = news; "__name" = xml; }
<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, STheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;">下方举另一个例子:</span><pre name="code" class="objc">#import "AppDelegate.h" #import "ViewController.h" #import "XMLDictionary.h" @implementation AppDelegate - (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(__unused NSDictionary *)launchOptions { NSURL *URL = [[NSURL alloc] initWithString:@"http://www.ibiblio.org/xml/examples/shakespeare/all_well.xml"]; NSString *xmlString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL]; NSLog(@"string: %@", xmlString); NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:xmlString]; NSLog(@"dictionary: %@", xmlDoc); self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } @end
上方NSDictionary *xmlContent = [NSDictionary dictionaryWithXMLString:dicItem[@"xmlContent"]];后打印出的xmlContent是:
{ ArticleCount = 1; Articles = { item = { ArticleId = 551; Description = "\U5f53\U5b69\U5b50\U7528\U4e0a\U767e\U5757\U7684\U73a9\U5177\U8f66\Uff0c\U6362\U56de\U4e86\U4fbf\U5b9c\U7684\U513f\U7ae5\U753b\Uff0c\U5bb6\U957f\U8be5\U5982\U4f55\U770b\U5f85\Uff1f\U662f\U7740\U773c\U4e8e\U7269\U54c1\U7684\U7ecf\U6d4e\U4ef7\U503c\Uff0c\U8fd8\U662f\U770b\U5230\U4ea4\U6362\U80cc\U540e\U7ed9\U5b69\U5b50\U5e26\U6765\U7684\U6210\U957f\Uff1f"; PicUrl = "http://nmpic.xxt.cn/201509/19221442817544072.jpg"; Title = "EQ\U52a0\U6cb9\U7ad9 \\| \U4ea4\U6362\U4e5f\U662f\U4e00\U79cd\U4ea4\U5f80\U65b9\U5f0f"; Url = "http://nmmp.xxt.cn/appajax/viewArticle.do?articleId=551&s=youbeitong.cn"; }; }; CreateTime = "2015-09-25 14:48:57"; FromMpId = 1; FromUserName = "\U7b2c\U4e00\U7236\U6bcd"; MsgType = news; "__name" = xml; }