以请求本地文件中的XML数据为例,说明iOS下如何用苹果自带类NSXMLParser解析XML数据。
本地文件students.xml中的数据格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<AllStudents>
<message>StudentInfo</message>
<student>
<name>Tom</name>
<age>10</age>
<school>JiangSu University</school>
</student>
<student>
<name>James</name>
<age>22</age>
<school>NanJing University</school>
</student>
<student>
<name>John</name>
<age>23</age>
<school>HongKong University</school>
</student>
</AllStudents>
详细解析步骤
.h文件
声明代理:<NSXMLParserDelegate>
定义属性:
@property(nonatomic,retain)NSString *currentValue;
@property(nonatomic,retain)NSMutableDictionary *messageDic;
@property(nonatomic,retain)NSMutableDictionary *studentInfoDic;
@property(nonatomic,retain)NSMutableArray *finalArray;
.m文件
@synthesize currentValue = _currentValue;
@synthesize messageDic = _messageDic;
@synthesize studentInfoDic = _studentInfoDic;
@synthesize finalArray = _finalArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 获取xml文件的路径
NSString*xmlPath=[[NSBundle mainBundle] pathForResource:@"students"ofType:@"xml"];
// 转化为Data
NSData *data = [[NSData alloc] initWithContentsOfFile:xmlPath];
// 初始化
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
// 代理
xmlParser.delegate = self;
// 开始解析
BOOL flag = [xmlParser parse];
if (flag) {
NSLog(@"解析成功");
NSLog(@"self.finalArray = %@",_finalArray);
}
else{
NSLog(@"解析出错");
}
[data release];
[xmlParser release];
}
//代理方法
//开始解析
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
// 用数组存储每一组信息
self.finalArray = [[[NSMutableArray alloc] init]autorelease];
}
//开始节点
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"message"]) {
self.messageDic = nil;
self.messageDic = [[[NSMutableDictionary alloc] initWithCapacity:0]autorelease];
}
if ([elementName isEqualToString:@"student"]) {
self.studentInfoDic = nil;
self.studentInfoDic = [[[NSMutableDictionary alloc]initWithCapacity:1]autorelease];
}
}
//发现节点值时
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
self.currentValue = string;//currentValue暂存节点值
}
//结束节点时
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"message"]) {
[self.messageDic setObject:_currentValue forKey:elementName];
//<message>元素存入数组
[self.finalArray addObject:_messageDic];
}
if ([elementName isEqualToString:@"name"]||
[elementName isEqualToString:@"age"]||
[elementName isEqualToString:@"school"])
{
//发现student的具体信息:age,name,school存入字典
[self.studentInfoDic setObject:_currentValue forKey:elementName];
}
if ([elementName isEqualToString:@"student"]) {
//<student>元素存入数组
[self.finalArray addObject:_studentInfoDic];
}
self.currentValue = @"";
}
最终的结果如下:
2014-06-02 14:03:02.104 XMLParser[5102:60b] 解析成功
2014-06-02 14:03:02.108 XMLParser[5102:60b] self.finalArray = (
{
message = StudentInfo;
},
{
age = 10;
name = Tom;
school = "JiangSu University";
},
{
age = 22;
name = James;
school = "NanJing University";
},
{
age = 23;
name = John;
school = "HongKong University";
}
)
以下总结来自:http://blog.csdn.net/like7xiaoben/article/details/7699965
注意:
解析xml数据的时候,
每当遇到元素节点的时候都会执行didStartElement代理方法,如果有属性节点,可以直接在这个方法中获取属性的值;
每当遇到文本节点的时候都会执行foundCharacters代理方法,获取文本节点中的值然后到didEndElement方法中进行区分。
如果是换行符的话也会获取,因为换行符也是文本节点,不过当一个元素节点结束后的换行符是不会获取的。
比如说: (换行符1)
<li>文本节点</li>
(换行符2)
元素节点前后各有一个换行符,这时只会获取换行符1,而不会获取换行符2。