在NSXMLParser中parsingxml

我已经阅读了很多如何从XML文件中获取文本的例子,但没有得到如何。 这是一个示例XML文件:

<?xml version="1.0" encoding="UTF-8"?> <questions> <set> <question>Question</question> <answer>Answer</answer> </set> </questions> 

使用-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI ,获取值的最简单方法是什么? 我已经有我的parsing器委托迷上了,所有这一切。

为了实现NSXMLParser,你需要实现它的委托方法。

首先以这种方式启动NSXMLParser。

 - (void)viewDidLoad { [super viewDidLoad]; rssOutputData = [[NSMutableArray alloc]init]; //declare the object of allocated variable NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@""]];// URL that given to parse. //allocate memory for parser as well as xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData]; [xmlParserObject setDelegate:self]; //asking the xmlparser object to beggin with its parsing [xmlParserObject parse]; //releasing the object of NSData as a part of memory management [xmlData release]; } //------------------------------------------------------------- -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict { if( [elementName isEqualToString:@"question"]) { strquestion = [[NSMutableString alloc] init]; } } //------------------------------------------------------------- -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { // init the ad hoc string with the value currentElementValue = [[NSMutableString alloc] initWithString:string]; } else { // append value to the ad hoc string [currentElementValue appendString:string]; } NSLog(@"Processing value for : %@", string); } //------------------------------------------------------------- -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if( [elementName isEqualToString:@"question"]) { [strquestion setString:elementName]; } [currentElementValue release]; currentElementValue = nil; } 

当遇到特定元素的结尾时,上面的委托方法被parsing器对象发送给它的委托。 在这个方法didEndElement你会得到question价值。

 // This one called out when it hit a starting tag on xml in your case <question> BOOL got = FALSE; - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI { if([elementName isEqualToString:@"question"]) { got = TRUE; } } // This is third one to called out which gives the end tag of xml in your case </question> - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName // This delegate is the second one to called out which gives the value of current read tag in xml - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(got) { NSLog(@"your desired tag value %@",string); got = FALSE; } } 

您需要实施该方法

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

一旦你看到你想要抓取的(内部)文本的元素,在你的程序中设置一个标志,并且保留一个string,在foundCharacters标签中findfind的字符。 一旦你点击了didEndElement方法,你可以对string做任何你想要的操作并重置标志。

例如

 -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if (sawQuestion) { // need to check here that self->myString has been initialized [self->myString appendString:string]; } } 

而在didEndElement您可以重置标志sawQuestion

您必须实现NSXMLParserDelegate的callback

关键的是:

 // called when it found an element - in your example, question or answer - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI // called when it hits the closing of the element (question or answer) - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI // called when it found the characters in the data of the element - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 

所以,当你点击元素时,你可以设置状态,分析器当前是在问题还是答案元素(带有iVar),然后当你用findCharacters调用时,基于你设置的元素的状态,你知道哪个variables(问题或答案)分配数据。