0

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

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

2 回答 2

1

好吧,我终于找到了我在这里缺少的东西。

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]);
于 2012-12-23T12:29:48.480 回答
0

您还可以从遍历所有元素的递归函数开始

- (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));
}
于 2014-10-20T17:45:35.580 回答