首页 > 代码库 > Objective-C学习之解析XML
Objective-C学习之解析XML
通过soap请求webservice时,返回的数据是XML类型,有时候也需要解析本地的xml数据等,苹果自带类NSXMLParser解析xml还是很方便的,简单轻便
本文以解析本地XML为例,网络获取到的返回值只需转换成NSData型,解析是同理
需要解析的xml文件如下,users.xml
01
<?xml version="1.0" encoding="UTF-8"?>
02
<AllUsers>
03
<message>用户信息</message>
04
<user>
05
<name>芳仔小脚印</name>
06
<age>10</age>
07
<school>JiangSu University</school>
08
</user>
09
<user>
10
<name>毒虫</name>
11
<age>22</age>
12
<school>NanJing University</school>
13
</user>
14
<user>
15
<name>女神</name>
16
<age>23</age>
17
<school>HongKong University</school>
18
</user>
19
</AllUsers>
我们用一个数组来存放,最终数据结构为
01
(
02
{
03
message = "用户信息";
04
},
05
{
06
age = 10;
07
name = "芳仔小脚印";
08
school = "JiangSu University";
09
},
10
{
11
age = 22;
12
name = "毒虫";
13
school = "NanJing University";
14
},
15
{
16
age = 23;
17
name = "女神";
18
school = "HongKong University";
19
}
20
)
解析步骤
一、声明代理 NSXMLParserDelegate
二、解析
01
// 遇到节点message和user时作为一个字典存放
02
NSArray *keyElements = [[NSArray alloc] initWithObjects:@"message",@"user", nil];
03
// 需要解析的字段
04
NSArray *rootElements = [[NSArray alloc] initWithObjects:@"message",@"name",@"age",@"school", nil];
05
// 获取xml文件的路径
06
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"users" ofType:@"xml"];
07
// 转化为Data
08
NSData *data = http://www.mamicode.com/[[NSData alloc] initWithContentsOfFile:xmlPath];
09
10
// 初始化
11
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
12
13
// 代理
14
xmlParser.delegate = self;
15
// 开始解析
16
BOOL flag = [xmlParser parse];
17
if (flag) {
18
NSLog(@"解析成功");
19
}
20
else{
21
NSLog(@"解析出错");
22
}
中间变量,在.m的interface的中定义
1
NSString *currentElement;
2
3
NSString *currentValue;
4
5
NSMutableDictionary *rootDic;
6
7
NSMutableArray *finalArray;
代理方法
01
#pragma - mark 开始解析时
02
-(void)parserDidStartDocument:(NSXMLParser *)parser
03
{
04
// 用数组存储每一组信息
05
finalArray = [[NSMutableArray alloc] init];
06
07
08
}
09
#pragma - mark 发现节点时
10
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
11
{
12
for(NSString *key in self.keyElements){
13
if ([elementName isEqualToString:key]) {
14
// 关键节点开始时,初始化一个字典来存放值
15
rootDic = nil;
16
17
rootDic = [[NSMutableDictionary alloc] initWithCapacity:0];
18
19
}
20
else {
21
for(NSString *element in self.rootElements){
22
if ([element isEqualToString:element]) {
23
currentElement = elementName;
24
currentValue = http://www.mamicode.com/[NSString string];
25
}
26
}
27
}
28
}
29
30
}
31
#pragma - mark 发现节点值时
32
33
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
34
{
35
36
if (currentElement) {
37
38
currentValue = http://www.mamicode.com/string;
39
[rootDic setObject:string forKey:currentElement];
40
}
41
42
}
43
#pragma - mark 结束节点时
44
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
45 javascript特效
{
46
if (currentElement) {
47
[rootDic setObject:currentValue forKey:currentElement];
48
currentElement = nil;
49
currentValue = http://www.mamicode.com/nil;
50
}
51
for(NSString *key in self.keyElements){
52