iphonexmltbxml

How to traverse this xml using tbxml


Can someone tell me how can I properly traverse the given xml structure using tbxml? I am using the attached code for TBXML parsing. I am able to get the values for id and name tag. But the method does not detect the values coming in title&description tags.

-(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));

}

The xml structure is -

  <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>

Solution

  • Well I finally found what I was missing here.

    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]);