Question

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>
Was it helpful?

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

OTHER TIPS

You can also start from a recursive function that will go through all elements

- (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));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top