I've got an xml file which i'm struggling to traverse through. I'll let you know from the off i'm new to 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.
Here's my XML File:
<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).
The .h file is:
@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.
My .M file has the following:
-(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;
}