NSXMLParser的使用

NSXMLParser解析xml格式的数据 用法如下:

 

首先,NSXMLParser必须继续NSXMLParserDelegate协议

 

@interface XMLHelper : NSObject <NSXMLParserDelegate>

 

首先设置XML数据,并初始化NSXMLParser

 

- (void)viewDidLoad {

NSString *strPathXml = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"xml"];

NSData *xmlData = [[NSData alloc] initWithContentsOfFile:strPathXml];

NSXMLParser *parserXML = [[NSXMLParser alloc] initWithData:xmlData];

[parser setShouldProcessNamespaces:NO];

[parser setShouldReportNamespacePrefixes:NO];

[parser setShouldResolveExternalEntities:NO];


[parser setDelegate:self];

[parser parse];

 

[super viewDidLoad];

}

 

 

 

//遍例xml的节点

 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

{

NSLog(@"Name:%@",elementName);

}

 

 

 

 

//当xml节点有值时,则进入此句 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

NSLog(@"Value:%@",string);

}

 

 

//当遇到结束标记时,进入此句

 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{


}

 

你可能感兴趣的:(xml,String,interface,attributes)