2

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;

}
Community
  • 1
  • 1
MissCoder87
  • 2,669
  • 10
  • 47
  • 82

1 Answers1

1

Based on the error, are you #including TBXML.h from the .h file? Alternatively, you could @class TBXMLElement; in the .h file and an import in the .m file.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
  • Hi, thanks for that. I was importing in the .m and nothing in the .h, i've put the @class in and it now compiles. But falls over with a SIGBRT error and puts a breakpoint in main.m at int retVal = UIApplicationMain(argc, argv, nil, nil); – MissCoder87 Aug 18 '11 at 13:33
  • Sounds like my answer helped. You should probably vote it up. :-) Any more information you can give about the error? Put the stack trace and error up. Did you remember to add libz.dylib? – Peter DeWeese Aug 18 '11 at 14:23
  • It helped, i don't have the rep to vote it up i'm afraid. The error says: int retVal = UIApplicationMain(argc, argv, nil, nil); with the SIGBART error and falls over. I think it's a bit of a red herring. Do you know if i've done my part .m bits right? – MissCoder87 Aug 18 '11 at 14:33
  • I've added my previous code to the end of my post with what i'm trying to achieve. Basically I want to do the same as that but loop through for every response to an array – MissCoder87 Aug 18 '11 at 14:50
  • I don't see the issue, but its really hard to debug by mental simulation. Use the debugger in Xcode. Set a breakpoint at the beginning of your method and step through one line at a time to find the culprit. – Peter DeWeese Aug 18 '11 at 16:07
  • I think i'm getting a bit confused with this one, i'll re-post what i've come to conclude – MissCoder87 Aug 18 '11 at 18:39