开发者

TBXML While Problem

Hey everbody, I'm getting some trouble with TBXML and While. I'm trying to parse a couple of nodes of XML. My XML is this:

<teste> 

1 2 3

But when I set while, my app just crashes.

    itemsList = [[NSMutableArray alloc] init];

 TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://localhost/dev/http/test.xml"]] retain];
 TBXMLElement * rootXMLElement = tbxml.rootXMLElement;

 TBXMLElement * comentarios = [TBXML childElementNamed:@"comentarios" parentElement:rootXMLElement];

 while (comentarios != nil) {
  TBXMLElement * comentario = [TBXML childElementNamed:@"comentario" parentElement:comentarios];

  NSString * descText = [TBXML textForElement:comentario];

  [itemsList addObject:descText];

}

Wha开发者_运维百科t im doing wrong?


You need to get the first child with childElementNamed, and then iterate over the rest via nextSiblingNamed. Here's a convenience method I have written that uses the new block API, might be interesting for you:

+(void) iterateSiblingsWithName:(NSString*)name forNodePath:(NSString*)path fromParent:(TBXMLElement*)node withBlock:(TBXMLElementIterator)block
{
    TBXMLElement* element = ( path != nil ) ? [TBXML nodeForPath:path fromParent:node] : node;
    if ( !element )
        {
                return;
        }
        TBXMLElement* valuenode = [TBXML childElementNamed:name parentElement:element];
        if ( !valuenode )
        {
                return;
        }
        do
        {
        block( valuenode );
        }
        while ( valuenode = [TBXML nextSiblingNamed:name searchFromElement:valuenode] );
}
@end


[itemlist retain];

i think it will solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜