Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-18-2011, 08:30 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 4
Skinzy is on a distinguished road
Unhappy Traversing XML with TBXML - Losing my mind!

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:

Code:
<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:

Code:
@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:

Code:
-(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.
Skinzy is offline   Reply With Quote
Old 08-18-2011, 08:35 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 4
Skinzy is on a distinguished road
Default

I've put @class TBXMLElement; in my .h now which stops the error but it falls over when I load the table
Skinzy is offline   Reply With Quote
Old 08-18-2011, 09:47 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 4
Skinzy is on a distinguished road
Default

If I go back to what I had before, it works but only for one item, how do I convert this to return to an array that loops through to get each one. At the moment it just lists the first one over and over again

Code:
-(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;

}
Skinzy is offline   Reply With Quote
Old 08-19-2011, 03:46 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 4
Skinzy is on a distinguished road
Default

Anyone?
Skinzy is offline   Reply With Quote
Old 08-19-2011, 04:53 AM   #5 (permalink)
el guaje
 
kevinyide's Avatar
 
Join Date: Jun 2010
Posts: 243
kevinyide is on a distinguished road
Send a message via Yahoo to kevinyide
Default

If its multiple <Location> elements that you want to loop through you need to look for more <Location> elements like
Code:
if (loc_element !=nil) // first <Location> element
{

	// extract child element values & store

	loc_element = [TBXML nextSiblingNamed:@"Location" searchFromElement:<rootelement>];
}
kevinyide is offline   Reply With Quote
Old 01-31-2012, 01:59 AM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

hi There, I will use Skinzy code because is very very similar to my code. I'm "fighting" with XML for a days and I decided to use TBXML..

first of all I think there should be

Code:
            TBXMLElement *id = [TBXML childElementNamed:@"CompanyID" parentElement:element];
instead of

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

but ok.. I got very similar program and XML so I wouldn't bother you by posting it there, so where I got up today?

I successfully loaded .xml file from internet asynchronously by using GREAT tutorial from http://www.iphonedevsdk.com/forum/ip...rvice-how.html - thank you dany_dev! unfortunatelly the next part of processing xml is "so hard" for me so I get TBXML and - I got all my records in the records = [NSMutableArray array]; so up to this is all ok..


but now: I just need to do something like

textlabel1.text = load company name for company 1
textlabel2.text = load location for company 1..

and so on.. (I'm using Skinzy example to don't mess the code..)

so anyone better than me with the NSMtableArray? my array has a data but.. how to reach the data?

thank you for your answers!

EDIT: OK I GOT IT! It is necessary to have separate NSMutableArray for every "item"..


my example code:

TBXMLElement *idcko = [TBXML childElementNamed:@"id" parentElement:element];
NSString *idckoS = [TBXML textForElement:idcko];
[idckoArray addObject:idckoS];

TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:element];
NSString *titleS = [TBXML textForElement:title];
[titleArray addObject:titleS];

Last edited by vogueestylee; 01-31-2012 at 03:51 AM.
vogueestylee is offline   Reply With Quote
Reply

Bookmarks

Tags
ios, sdk 4, tbxml, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 391
14 members and 377 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jimmyon122, jonathandeknudt, pungs, raymng, stanny, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0