我研究了NSXMLParser,然後准備實現一下,解析XML文件,代碼如下:
-(OneNodeXMLParser*)initOneNodeXMLParser{
appDelegate = (OneNodeXMLParser*)[[UIApplication sharedApplication]delegate];
return self;
}
-(void)parser:(NSXMLParser*) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"%s",__PRETTY_FUNCTION__,nil);
if([elementName isEqualToString:@"a"]){
}
NSLog(@"Starting processing");
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"%s",__PRETTY_FUNCTION__,nil);
NSLog(@"%s",string);
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"%s",__PRETTY_FUNCTION__,nil);
NSLog(@"Finishing processing");
}
要成功解析應該改哪啊?
在.h文件中聲明一個NSMutableString *store;
然後加下面的代碼:
-(void)parser:(NSXMLParser*) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"a"])
{
store = [[NSMutableString alloc] init];
}
NSLog(@"Starting processing");
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"%s",string);
if (store != nil)
{
[store appendString:string];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"Finishing processing String : %@", store);
}