有人能告诉我如何使用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>
答案 0 :(得分: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]);
答案 1 :(得分: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));
}