NSXMLParser解析xml格式的数据 用法如下:
首先,NSXMLParser必须继续NSXMLParserDelegate协议
@interface XMLHelper : NSObject <NSXMLParserDelegate>
首先设置XML数据,并初始化NSXMLParser
- (void)viewDidLoad { NSMutableString *Strxml=[NSMutableString stringWithString:@"<Body><xml1>11111111</xml1><xml2><xml3>3333333333</xml3></xml2></Body>"]; NSData *data=[NSData dataWithBytes:[Strxml UTF8String] length:[Strxml length]]; [parser setShouldProcessNamespac [parser setShouldReportNamespace [parser setShouldResolveExternal [parser parse]; [super viewDidLoad]; } |
//遍例xml的节点
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Name:%@",elementName);
}
//当遇到结束标记时,进入此句
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { } |