作为初学者,我已经阅读了NSXMLParser
并尝试实现解析器来解析xml
<?xml version="1.0" encoding="UTF-8"?>
<a>Some text here</a>
我的实施
-(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"]){
// init some varibles
}
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");
}
我应该修改什么才能成功解析文件?
答案 0 :(得分:1)
在.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);
}
您可以使用该字符串变量store
以供将来使用。