我有一个xml文件,我正在努力遍历。从一开始我会告诉你我是xcode的新手。我试着在这里遵循另一个指南:
iPhone TBXML循环和解析数据,但由于某种原因,它似乎不适合我。
这是我的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
然而,我得到了2个错误的说法:'期望一个类型和期望')' TBXMLElement之前。
我的.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;
}
基于错误,您是否#包括.h文件中的TBXML.h ?或者,您可以在.h文件中添加@class TBXMLElement;
,并在.m文件中导入。