Magnus
Magnus

Reputation: 1442

Array count is 1 after XML parsing iPhone

I'm parsing flight times in my iPhone app.

Here's a snippet from the XML

<?xml version="1.0" encoding="iso-8859-1"?>
<airport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<flights lastUpdate="2011-11-07T10:49:43">
<flight uniqueID="2131560">
  <airline>DY</airline>
  <flight_id>DY1052</flight_id>
  <dom_int>S</dom_int>
  <schedule_time>2011-11-04T06:00:00</schedule_time>
  <arr_dep>D</arr_dep>
  <airport>SVG</airport>
  <via_airport>CPH</via_airport>
  <check_in>C</check_in>
</flight>
<flight uniqueID="2136807">
  <airline>SK</airline>
  <flight_id>SK308</flight_id>
  <dom_int>D</dom_int>
  <schedule_time>2011-11-07T07:15:00</schedule_time>
  <arr_dep>D</arr_dep>
  <airport>SVG</airport>
  <via_airport>SVG</via_airport>
  <check_in>EF</check_in>
</flight>
...

I store each flight in a Flight object which looks like this:

@interface Flights : NSObject{
NSInteger flightUniqueID;
NSString *airline;
NSString *flight_id;
NSString *dom_int;
NSDate *schedule_time;
NSString *arr_dep;
NSString *airport;
NSString *check_in;
NSString *status;
NSString *via_airport;
NSString *gate;
NSString *delayed;

}
@property(nonatomic, readwrite)NSInteger flightUniqueID;
@property(nonatomic, retain)NSString *airline;
@property(nonatomic, retain)NSString *flight_id;
@property(nonatomic, retain)NSString *dom_int;
@property(nonatomic, retain)NSDate *schedule_time; 
@property(nonatomic, retain)NSString *arr_dep;
@property(nonatomic, retain)NSString *airport;
@property(nonatomic, retain)NSString *check_in;
@property(nonatomic, retain)NSString *status;
@property(nonatomic, retain)NSString *via_airport;
@property(nonatomic, retain)NSString *gate;
@property(nonatomic, retain)NSString *delayed;

I use an NSXMLParser to parse the XML

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict{

if ([elementName isEqualToString:@"airport"]) {
    allFlights = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:@"flight"]){
    flight = [[Flights alloc]init];

    flight.flightUniqueID = [[attributeDict objectForKey:@"uniqueID"]integerValue];

}

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
  if(!currentElementValue) 
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];


}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName isEqualToString:@"flights"]) {
    return;
}
if ([elementName isEqualToString:@"flight"]) {
    NSLog(@"Adding object: %@" , flight);
    [allFlights addObject:flight];

    [flight release];
    flight = nil;
}else
    [flight setValue:currentElementValue forKey:elementName];

   [currentElementValue release];
currentElementValue = nil;
}

When i NSLog "flight", I get this output:

Adding object: <Flights: 0xsomenumber>
...
Adding object: <Flights: 0x7064360>

which goes on for however many flights there are in the xml.

However, the array I add each object to only stores the last flight object. If I try to NSLog the array after the parsing is done, it prints out the last object, and nothing else.

Obviously I need to have all the flights stored in the array, so I can show all of them in my tableview after the parsing is done.

How can I achieve this? Am I missing something?

I tried to parse the XML without releasing the flight object, but that didn't work either.

Upvotes: 0

Views: 291

Answers (2)

HarshIT
HarshIT

Reputation: 4915

After the completion of parsing operation , try

NSLog(@"the number of objects are -> %d", [Your Array object count]);

and see what does it show ?????

If it shows correct number of objects , that means all the objects are added. and you may print them using looping like

for (Flight *objFlight in arrFlights)
{
    NSLog (@"Flight Name -> %@",objFlight.strFlihtName);
}

Happy Coding...

Upvotes: 0

Daniel Hepper
Daniel Hepper

Reputation: 29957

You set allFlights to an empty array upon airport elements. My guess is that you want to initialize it once upon start.

However, there are nested airport elements in each flight element, so you end up only with the last flight. Try initializing allFlights somewhere else.

Upvotes: 1

Related Questions