关键方法:
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": "
1 2015-09-25 14:48:57 1 ",
"mpId": "1"
},
{
"logo": "http://nmmp.xxt.cn/mp/logo/2.png",
"mpName": "校讯通助手",
"xmlContent": "
2 2015-10-12 15:23:11 1 ",
"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];
就是因为我没有看返回的内容,所以直接xmlContent[@"Description"]; 导致错误好几次,下方正确的
NSDictionary *xmlContent = [NSDictionary dictionaryWithXMLString:dicItem[@"xmlContent"]];
NSDictionary *messageContentOne= xmlContent[@"Articles"];
NSDictionary *messageContentTwo = messageContentOne[@"item"];
item.messageContent = messageContentTwo[@"Description"];
这里是XML文件转换为NSDictionary的文件:以后认真的根据这个来写字典输出的内容:
{
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;
}
下方举另一个例子:#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;
}