如何使用tbxml遍历此xml

问题描述:

有人能告诉我如何正确遍历使用tbxml的给定xml结构?我正在使用附加的代码进行TBXML解析。我能够获得id和名称标签的值。但该方法不检测标题&描述标签中的值。如何使用tbxml遍历此xml

-(void)traverseElement:(TBXMLElement *)element 
{ 
do 
{ 
    [TBXML iterateAttributesOfElement:element withBlock:^(TBXMLAttribute *attribute, NSString *name, NSString *value) { 
     NSLog(@"%@->%@ = %@",[TBXML elementName:element], name, value); 
    }]; 

    if (element->firstChild) 
     [self traverseElement:element->firstChild]; 
} 
while ((element = element->nextSibling)); 

}

的XML结构 -

<allresponse> 
    <responses> 
     <response id="123" name ="myname1"> 
      <title> Some Title1 </title> 
      <description>Some decription 1</description> 
     </response> 
     <response id="456" name ="myname2"> 
      <title> Some Title2 </title> 
      <description>Some decription 2</description> 
     </response> 
    </responses> 
</allresponse> 

好吧,我终于找到了我失踪了这里。

TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:element]; 
      NSLog(@"Title is %@",[TBXML textForElement:title]); 

TBXMLElement *description = [TBXML childElementNamed:@"description" parentElement:element]; 
      NSLog(@"Description is %@",[TBXML textForElement:description]); 

您也可以从一个递归函数,将通过所有的元素开始

- (void) traverseElement:(TBXMLElement *)element { 

    do { 
     if (element->firstChild) 
      [self traverseElement:element->firstChild]; 

     //do whatever you think is useful to you here 

    } while ((element = element->nextSibling)); 
}