【问题标题】:How to traverse this xml using tbxml如何使用 tbxml 遍历这个 xml
【发布时间】:2012-12-10 04:07:50
【问题描述】:

谁能告诉我如何使用 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>

【问题讨论】:

    标签: iphone xml tbxml


    【解决方案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]);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多