在NSXMLParser中解析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,获得值QuestionAnswer的最简单方法是什么?我已经连接了我的解析器代理,诸如此类。

为了实现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;
}

当解析器对象遇到特定元素的末尾时,会将上面的委托方法发送给它的委托。在这个方法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

一旦您看到要获取其(内部)文本的元素,请在程序中设置一个标志,并保留一个包含foundCharacters在标记之间找到的内容的字符串。一旦达到didEndElement方法,就可以对字符串执行所需操作并重置标志。

例如

-(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 的回调

关键是:

// 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),然后当你用foundCharacters被调用时,根据你点击元素设置的状态,你知道要将数据分配给哪个变量(问答)。

最新更新