【问题标题】:Traversing through TBXML on iphone / xcode SDK help?在 iphone / xcode SDK 上遍历 TBXML 有帮助吗?
【发布时间】:2011-10-29 19:01:57
【问题描述】:

我有一个 xml 文件,我正在努力遍历它。我会从一开始就告诉你我是 xcode 的新手。我尝试在此处遵循另一个指南:

iPhone TBXML Looping And Parsing Data 但由于某种原因,它似乎对我不起作用。

这是我的 XML 文件:

<Locations action="Request" version="1.0">
     <location>
       <CompanyID>44502</CompanyID>
       <CompanyName>Holiday Inn</CompanyName>
       <Distance>3.58km/2.23miles</Distance>
         <tabs>
           <tab>General Information</tab>
           <tab>Add Review</tab>
         </tabs>
       <Address>
           <Address1>Holiday Inn Reading - South</Address1>
           <Address2>500 Basingstoke Road</Address2>
           <Address3/>
           <PostTown>Reading</PostTown>
           <County/>
           <PostCode>RG2 0SL</PostCode>
       </Address>
     </location>
</Locations>

我正在尝试使用 TBXML 遍历它以获取每个项目(我设法用一个来完成,但无法循环)。

.h 文件是:

@interface LoadXML : UIViewController {

    IBOutlet UITableView *tblLoadXML;
    NSMutableArray *records; 
    }
@property(nonatomic,retain)NSMutableArray *records;

-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;

@end

但是,我在 TBXMLElement 之前遇到了 2 个错误:`Expected a type and Expected ')'。

我的 .M 文件有以下内容:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (Cell == nil) {
        Cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }


    Cell.text = [records objectAtIndex:indexPath.row];

}

- (void)loadRecords:(NSString *)records {
    NSString *someXML = @"http://de0937/directenquirieswebapplicationv3.0/mobilesearch/what/0/49424/342/0/Reading/Hotels%20and%20Inns/0/0/0/0/0/search.aspx";
    TBXML *tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:someXML]] retain];

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

    if (tbxml.rootXMLElement)
        [self traverseElement:tbxml.rootXMLElement];
    [tbxml release];
}

- (void) traverseElement:(TBXMLElement *)element {
    do {
        if (element->firstChild) 
            [self traverseElement:element->firstChild];

        if ([[TBXML elementName:element] isEqualToString:@"location"]) {
            TBXMLElement *name = [TBXML childElementNamed:@"CompanyName" parentElement:element];
            TBXMLElement *distance = [TBXML childElementNamed:@"Distance" parentElement:element];

            TBXMLElement *id = [TBXML childElementNamed:@"ID" parentElement:element];

            [records addObject:[NSArray arrayWithObjects:
                                [TBXML textForElement:name],
                                [TBXML textForElement:distance],
                                [TBXML textForElement:id],nil]];  
        }
    } while ((element = element->nextSibling));  
}

我可能正在做一些非常愚蠢的事情,但这让我发疯了!任何帮助将不胜感激。

--- 这是我以前的,它工作得很好,但只适用于一张唱片。基本上我要做的是转换它,这样我就可以将每个项目作为一个数组并随意拉出:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (Cell == nil) {
        Cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }


    TBXML * XML = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.tomstest.info/ios/results.xml"]] retain];

    TBXMLElement *rootXML = XML.rootXMLElement;
    TBXMLElement *results = [TBXML childElementNamed:@"location" parentElement:rootXML];  
    TBXMLElement *WOEID = [TBXML childElementNamed:@"CompanyName" parentElement:results]; 
    NSString *woeid = [TBXML textForElement:WOEID];


    Cell.text = woeid;
    return Cell;

}

【问题讨论】:

    标签: iphone ios xcode tbxml


    【解决方案1】:

    根据错误,您是在 .h 文件中#include TBXML.h 吗?或者,您可以在 .h 文件中 @class TBXMLElement; 并在 .m 文件中导入。

    【讨论】:

    • 嗨,谢谢。我在 .m 中导入,在 .h 中没有任何内容,我已将 @class 放入其中,现在可以编译。但是由于 SIGBRT 错误而摔倒,并在 main.m 中的 int retVal = UIApplicationMain(argc, argv, nil, nil); 处放置一个断点;
    • 听起来我的回答有帮助。你可能应该投票赞成。 :-) 您可以提供有关该错误的更多信息吗?把堆栈跟踪和错误放在上面。你记得添加 libz.dylib 吗?
    • 它有帮助,恐怕我没有代表投票赞成。错误说: int retVal = UIApplicationMain(argc, argv, nil, nil);出现 SIGBART 错误并摔倒。我认为这有点像红鲱鱼。你知道我是否已经完成了我的部分 .m 位吗?
    • 我已经将我之前的代码添加到我想要实现的帖子的末尾。基本上我想做同样的事情,但循环遍历对数组的每个响应
    • 我没有看到问题,但是通过心理模拟真的很难调试。在 Xcode 中使用调试器。在方法的开头设置一个断点,一次通过一行来找到罪魁祸首。
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 2012-08-12
    • 2011-09-26
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多