1

我尝试使用 TBXML 解析以下字符串。

<panel><start><post_id>4</post_id><user_id>2</user_id><post>Hyder here</post><created_on>2012-01-09 06:36:59</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>3</post_id><user_id>2</user_id><post>Hello hyder here</post><created_on>2012-01-09 06:34:09</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>2</post_id><user_id>0</user_id><post>Hi, This is Syed Hyder....</post><created_on>2012-01-09 01:07:36</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>1</post_id><user_id>0</user_id><post>Hello, gaurav....How are you.</post><created_on>2012-01-09 01:05:11</created_on><likes>0</likes><noc>0</noc><status>A</status></start></panel>

我的代码: -

NSMutableArray *newArr = [[NSMutableArray alloc] init];//21
    RXMLElement *rxml = [[RXMLElement alloc] initFromXMLString:response];
    [rxml iterate:@"panel.start" with:^(RXMLElement *start) {
        ModelPost *newPost = [[ModelPost alloc] init];
        newPost.message = [NSString stringWithFormat:@"%@", [start child:@"post"]];
        NSLog(@"Post = %@", [start child:@"post"]);
        [newArr addObject:newPost];
    }];

但我无法做到这一点或进入iterate loop. 请通过示例代码、建议和教程帮助我解决这个问题。提前致谢....

4

1 回答 1

2
 - (void)xmlparserurl{

//xml data url calling....

    tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://karen.webmascot.com/iapi/orders.php?max_count=10"]] retain];


records = [NSMutableArray array];
[records retain];




//retrieving all data from the xml
if (tbxml.rootXMLElement){
    NSLog(@"inserting");
    [self traverseElement:tbxml.rootXMLElement];
}
[tbxml release];
}

- (void)traverseElement:(TBXMLElement *)element {
do {
    //NSLog(@"%@",[TBXML elementName:element]);
    if (element->firstChild) 
        [self traverseElement:element->firstChild];
    if ([[TBXML elementName:element] isEqualToString:@"start"]) {
        //NSLog(@"xml element checking");
        TBXMLElement *id = [TBXML childElementNamed:@"attribute name" parentElement:element];

//inserting into the mutable array      
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:elementname],
[TBXML textForElement:elementname],
[TBXML textForElement:elementname],nil]];

    }

} while ((element = element->nextSibling)); 


[self.tableview reloadData];
}

我只发送检索 xml 数据的代码。我还将发送教程,您将在其中获得进一步问题的答案。

http://www.tbxml.co.uk/TBXML/API.html

于 2012-01-10T08:39:40.663 回答