原文地址:http://blog.csdn.net/smallmuou/article/details/7404330
一、概述
由于xml具有强大的扩展性及清晰的结构,使其广泛用于各种文件配置,比如皮肤配置;那么在IOS该如何解析xml文件呢?xml解析必然要用到xml解析库,有libxml、tinyxml等,但在IOS的API中提供了NSXMLParser类,通过NSXMLParser可以实现快速访问xml中相关字段;
二、NSXMLParser类几个重要知识点
1、要使用NSXMLParser类,必须遵循NSXMLParserDelegate协议,即在自身类声明时,必须加上<NSXMLParserDelegate>
2、必须设置代理,这样才能够访问自身重定义的方法,即才能处理xml中数据;
3、以下对NSXMLParserDelegate协议中几个常用的方法进行说明:
- (void)parserDidStartDocument:(NSXMLParser *)parser;
该方法将在开始parse之前调用,用处是可以在其中对相应的对象进行分配和初始化;
- (void)parserDidEndDocument:(NSXMLParser *)parser;
该方法将在parse结束后调用,用处是进行相应的资源释放和处理;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
该方法将在parse期间碰到元素开始标志"<xxx"时调用,用于元素属性获取,其中elementName为元素名,attributeDict为相应的属性集合;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
该方法是在遇到元素结束标志"</xxx>"调用;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
该方法是遇到元素值时调用,通过它可以获取元素内容;
三、实例
千言万语也许也不如代码直观,现在就将用代码说话,假设要解析的xml文件如下:
/***************************************************************
** skin.xml **
***************************************************************/
<?xml version="1.0" encoding="utf-8"?>
<skin>
<string name="title" id="1">学习xml解析</string>
<string name="content" id="1">hello world</string>
</skin>
<skin>
<string name="title" id="1">学习xml解析</string>
<string name="content" id="1">hello world</string>
</skin>
/*************************************************************************
** skin.h **
*************************************************************************/
#import <Foundation/Foundation.h>
@interface SKIN : NSObject <NSXMLParserDelegate>
{
NSMutableArray *viewArray;//用于存放资源
NSString *skinName;
}
-(BOOL)load:(NSString *)name;
-(void)unload;
@end
@interface SKIN : NSObject <NSXMLParserDelegate>
{
NSMutableArray *viewArray;//用于存放资源
NSString *skinName;
}
-(BOOL)load:(NSString *)name;
-(void)unload;
@end
/***************************************************************
** skin.m **
***************************************************************/
#import "skin.h"
@implementation SKIN
-(BOOL)load:(NSString *)name
{
//read file to buffer
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:name];
NSData *data = [file readDataToEndOfFile];
[file closeFile];
//create xml parser
NSXMLParser *parser;
parser = [[NSXMLParser alloc] initWithData:data];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser setDelegate:self];
//start parse
[parser parse];
return YES;
}
-(void)unload
{
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//alloc
NSLog(@"=======start parse=========");
viewArray = [[NSMutableArray alloc] init];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"=======end parse=========");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"=======start element:%@=========", elementName);
if ([elementName isEqualToString:@"skin"]) {
NSLog(@"skin name=%@", [attributeDict objectForKey:@"name"]);
} else if ([elementName isEqualToString:@"string"]) {
NSLog(@"string name= %@ id=%@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"id"]);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"=======end element:%@=========", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"=======element content=%@=====", string);
}
@end
@implementation SKIN
-(BOOL)load:(NSString *)name
{
//read file to buffer
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:name];
NSData *data = [file readDataToEndOfFile];
[file closeFile];
//create xml parser
NSXMLParser *parser;
parser = [[NSXMLParser alloc] initWithData:data];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser setDelegate:self];
//start parse
[parser parse];
return YES;
}
-(void)unload
{
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//alloc
NSLog(@"=======start parse=========");
viewArray = [[NSMutableArray alloc] init];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"=======end parse=========");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"=======start element:%@=========", elementName);
if ([elementName isEqualToString:@"skin"]) {
NSLog(@"skin name=%@", [attributeDict objectForKey:@"name"]);
} else if ([elementName isEqualToString:@"string"]) {
NSLog(@"string name= %@ id=%@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"id"]);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"=======end element:%@=========", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"=======element content=%@=====", string);
}
@end
/***************************************************************
** main.m **
***************************************************************/
#import <Foundation/Foundation.h>
#import "skin.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
SKIN *skin = [[SKIN alloc] init];
[skin load:@"/Extra/skin_cn.xml"];
[skin unload];
}
return 0;
}
#import "skin.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
SKIN *skin = [[SKIN alloc] init];
[skin load:@"/Extra/skin_cn.xml"];
[skin unload];
}
return 0;
}
执行结果:
2012-03-28 19:41:20.720 test[2321:707] =======start parse=========
2012-03-28 19:41:20.722 test[2321:707] =======start element:skin=========
2012-03-28 19:41:20.723 test[2321:707] skin name=(null)
2012-03-28 19:41:20.724 test[2321:707] =======element content=
=====
2012-03-28 19:41:20.725 test[2321:707] =======start element:string=========
2012-03-28 19:41:20.726 test[2321:707] string name= title id=1
2012-03-28 19:41:20.728 test[2321:707] =======element content=学习xml解析=====
2012-03-28 19:41:20.729 test[2321:707] =======end element:string=========
2012-03-28 19:41:20.730 test[2321:707] =======element content=
=====
2012-03-28 19:41:20.731 test[2321:707] =======start element:string=========
2012-03-28 19:41:20.732 test[2321:707] string name= content id=1
2012-03-28 19:41:20.733 test[2321:707] =======element content=hello world=====
2012-03-28 19:41:20.734 test[2321:707] =======end element:string=========
2012-03-28 19:41:20.735 test[2321:707] =======element content=
=====
2012-03-28 19:41:20.736 test[2321:707] =======end element:skin=========
2012-03-28 19:41:20.737 test[2321:707] =======end parse=========
2012-03-28 19:41:20.722 test[2321:707] =======start element:skin=========
2012-03-28 19:41:20.723 test[2321:707] skin name=(null)
2012-03-28 19:41:20.724 test[2321:707] =======element content=
=====
2012-03-28 19:41:20.725 test[2321:707] =======start element:string=========
2012-03-28 19:41:20.726 test[2321:707] string name= title id=1
2012-03-28 19:41:20.728 test[2321:707] =======element content=学习xml解析=====
2012-03-28 19:41:20.729 test[2321:707] =======end element:string=========
2012-03-28 19:41:20.730 test[2321:707] =======element content=
=====
2012-03-28 19:41:20.731 test[2321:707] =======start element:string=========
2012-03-28 19:41:20.732 test[2321:707] string name= content id=1
2012-03-28 19:41:20.733 test[2321:707] =======element content=hello world=====
2012-03-28 19:41:20.734 test[2321:707] =======end element:string=========
2012-03-28 19:41:20.735 test[2321:707] =======element content=
=====
2012-03-28 19:41:20.736 test[2321:707] =======end element:skin=========
2012-03-28 19:41:20.737 test[2321:707] =======end parse=========
四、总结
以上例子自是个引子,更多接口可以查看相关帮助;