[英]Traversing through TBXML on iphone / xcode SDK help?
I've got an xml file which i'm struggling to traverse through. 我有一个要遍历的xml文件。 I'll let you know from the off i'm new to xcode.
我会让您从头开始了解xcode。 I tried following another guide on here:
我在这里尝试遵循另一指南:
iPhone TBXML Looping And Parsing Data but for some reason it doesn't seem to work for me. iPhone TBXML循环和解析数据,但由于某种原因,它似乎对我不起作用。
Here's my XML File: 这是我的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>
I'm trying to traverse through this with TBXML to grab each of the items (I managed to do it with one, but couldn't loop through). 我正在尝试使用TBXML遍历所有内容(我设法做到了其中一项,但无法循环通过)。
The .h file is: .h文件是:
@interface LoadXML : UIViewController {
IBOutlet UITableView *tblLoadXML;
NSMutableArray *records;
}
@property(nonatomic,retain)NSMutableArray *records;
-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;
@end
However I'm getting 2 errors on that saying: `Expected a type and Expected ')' before TBXMLElement. 但是我在这句话上遇到了2个错误:TBXMLElement之前有'expected a type and Expected')'。
My .M file has the following: 我的.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));
}
I'm probably doing something very stupid, but it's driving me insane! 我可能正在做一些非常愚蠢的事情,但这使我发疯! Any help will be much appreciated.
任何帮助都感激不尽。
--- This is what I had before, and it works perfectly but only for one record. ---这是我以前的经验,它可以完美运行,但仅用于一项记录。 Basically what i'm trying to do is convert this so I can put each item as an array and pull it out at will:
基本上我想做的就是将其转换,这样我就可以将每个项目作为一个数组并将其随意拉出:
-(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;
}
Based on the error, are you #including TBXML.h from the .h file? 基于该错误,您是否#.h文件中包含TBXML.h? Alternatively, you could
@class TBXMLElement;
或者,您可以
@class TBXMLElement;
in the .h file and an import in the .m file. 在.h文件中导入,并在.m文件中导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.