Note that there are some explanatory texts on larger screens.

plurals
  1. POxcode 4.2 Loop Through XML Data Elements
    primarykey
    data
    text
    <p>I am very confused on how I am supposed to loop through XML elements for my current iPhone app project. I will show my code, then clarify my question.</p> <p>Here is sample XML I am using:</p> <pre><code>&lt;restaurant_details&gt; &lt;total_results&gt;90&lt;/total_results&gt; &lt;restaurant&gt; &lt;name&gt;Through the Garden&lt;/name&gt; &lt;distance_from_current_location&gt;0.55&lt;/distance_from_current_location&gt; &lt;restaurant_id&gt;123&lt;/restaurant_id&gt; &lt;longitude&gt;-84.373734&lt;/longitude&gt; &lt;latitude&gt;39.258606&lt;/latitude&gt; &lt;address&gt;10738 Kenwood Rd | cincinnati ,OH | 45242&lt;/address&gt; &lt;phone_number&gt;513-791-2199&lt;/phone_number&gt; &lt;restaurant_type&gt;General&lt;/restaurant_type&gt; &lt;/restaurant&gt; &lt;/restaurant_details&gt; </code></pre> <p>Getting data for my table:</p> <pre><code>- (void)get_table_data { NSString *day = [self dateInFormat:@"%A"]; NSLog (@"At get_table_data"); NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.url.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { NSLog (@"At connection"); receivedData = [NSMutableData data]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [receivedData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { // NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding]; // NSLog(@"%@",theXML);[theXML release]; if(parser){ parser = nil; } parser = [[NSXMLParser alloc] initWithData: receivedData]; [parser setDelegate: self]; [parser setShouldResolveExternalEntities: YES]; [parser parse]; } </code></pre> <p>And my parser code:</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ NSLog (@"At parser"); currentElement = elementName; if ([currentElement isEqualToString:@"restaurant_details"]) { if ([currentElement isEqualToString:@"total_results"]) { //NSLog(@"Element: %@", currentElement); }else if ([currentElement isEqualToString:@"restaurant"]) { restaurantObj = [[DOR_RestaurantClass alloc]init]; } } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if([currentElement isEqualToString:@"name"]) { restaurantObj.name=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"distance_from_current_location"]) { restaurantObj.distance=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"restaurant_id"]) { restaurantObj.restId=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"address"]) { NSArray *stringArray = [string componentsSeparatedByString:@" | "]; restaurantObj.address=[stringArray objectAtIndex:0]; restaurantObj.address2=[stringArray objectAtIndex:1]; restaurantObj.address3=[stringArray objectAtIndex:2]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"phone_number"]) { restaurantObj.phone=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"description"]) { restaurantObj.description=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"image_url"]) { restaurantObj.image=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } if([currentElement isEqualToString:@"restaurant_type"]) { //restaurantObj.Name=[NSString stringWithFormat:@"%@",string]; //NSLog(@"Name to be saved in Array :- %@",string); } NSLog (@"At parser2"); if(!currentElementValue) currentElementValue=[[NSMutableString alloc] initWithString:string]; else [currentElementValue appendString:string]; NSLog(@"the parser just found this text in a tag:%@",string); } -(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName { NSLog(@"Current element in End Element Category:- %@",currentElement); if([elementName isEqualToString:@"restaurant"]) { [listItems addObject:restaurantObj]; NSLog(@"Array: %@", listItems); }else{ currentElementValue=nil; } } </code></pre> <p>Here's my .h for my restaurant class:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface DOR_RestaurantClass : NSObject @property (nonatomic, retain) NSString *restId; @property (nonatomic, retain) NSString *image; @property (nonatomic, retain) NSString *name; @property (nonatomic, retain) NSString *description; @property (nonatomic, retain) NSString *distance; @property (nonatomic, retain) NSString *address; @property (nonatomic, retain) NSString *address2; @property (nonatomic, retain) NSString *address3; @property (nonatomic, retain) NSString *phone; @property (nonatomic, retain) NSString *expires; @end </code></pre> <p>And my .m:</p> <pre><code>#import "DOR_RestaurantClass.h" @implementation DOR_RestaurantClass @synthesize restId; @synthesize image; @synthesize name; @synthesize description; @synthesize distance; @synthesize address; @synthesize address2; @synthesize address3; @synthesize phone; @synthesize expires; @end </code></pre> <p>I am getting my data and so forth, so that's not an issue. My question is this: With multiple "restaurant" tags in my XML data, how do I sort through the XML so that I keep restaurant data together? I come from PHP, so I would have done a foreach on the restaurant tags, but I'm not sure how the NSXMLParser is supposed to keep the data seperated. I will be putting this information into an NSMutableArray in the end. I just don't know where to start, and cannot find any good examples on doing this. Any and all help is greatly appreciated.</p> <p><strong>This has been updated</strong></p> <p>My array <code>listItems</code> (NSMutableArray) is <code>null</code> when I print it to <code>NSLog</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload