如何在iphone sdk中使用TBXML解析字符串?

时间:2012-01-10 06:53:47

标签: objective-c iphone-sdk-4.1 tbxml

我尝试使用TBXML解析以下String。

<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。 请通过示例代码,建议和教程帮助我解决这个问题。 在此先感谢....

1 个答案:

答案 0 :(得分: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