tags:

views:

8

answers:

0

I'm attempting to parse a large amount of XML data from a SOAP web service and then display it in a UITableView.

When the service is called, this is the XML output from NSLog:

<Country><Name>Argentina</Name><Capital>Buenos Aires</Capital></Country>
<Country><Name>Australia</Name><Capital>Canberra</Capital></Country>
<Country><Name>Brazil</Name><Capital>Brasilia</Capital></Country>
<Country><Name>Canada</Name><Capital>Ottawa</Capital></Country>
<Country><Name>Costa Rica</Name><Capital>San Jose'</Capital></Country>

Here's the offending code.

.h

NSMutableArray *countryListArray; // main array used in tableview
NSMutableDictionary *countryDict; // a temporary item added to the "countryListArray" array one at a time, and cleared for the next one

// this parses through the document, from top to bottom...
// collects and caches each sub-element value, and then save each item to our array.
// we use these to track each current item, until it's ready to be added to the "countryListArray" array
NSString *currentCountry;
NSMutableString *currentName;
NSMutableString *currentCapital;

.m

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

    currentCountry = [elementName copy];

    if( [elementName isEqualToString:@"Country"]) {

        countryDict = [[[NSMutableDictionary alloc] init] autorelease];
        currentName = [[[NSMutableString alloc] init] autorelease];
        currentCapital = [[[NSMutableString alloc] init] autorelease];
    }
}

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

    if ([elementName isEqualToString:@"Country"]) {
        [countryDict setObject:currentName forKey:@"Name"];
        [countryDict setObject:currentCapital forKey:@"Capital"];

        [countryListArray addObject:[countryDict copy]];

        NSLog(@"countryListArray= %@", countryListArray);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"found characters: %@", string);
        if ([currentCountry isEqualToString:@"Name"]) {
            [currentName appendString:string];
        } else if ([currentCountry isEqualToString:@"Capital"]) {
            [currentCapital appendString:string];
        }
}

- (void)connectionDidFinishLoading:(NSURLConnection *) connection {
    countryListArray = [[NSMutableArray alloc] init];
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

        //---shows the XML---
    NSLog(@"theXML=%@",theXML);
    [theXML release];   
    if (xmlParser){
        [xmlParser release];
    }   

    xmlParser = [[[NSXMLParser alloc] initWithData:webData] autorelease];

    [xmlParser setDelegate: self];
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];

    [connection release];
    [webData release];

    NSLog(@"countryListArray has %d items", [countryListArray count]);
}

My issue this that this bit of code doesn't parse the data as I expect it to. Instead of this(Do Want):

countryListArray=(
{Name = "Argentina"; Capital = "Buenos Aires";}, 
{Name = "Australia"; Capital = "Canberra";} ,
{Name = "Brazil"; Capital = "Brasilia";} ,
{Name = "Canada"; Capital = "Ottawa";} ,
{Name = "Costa Rica"; Capital = "San Jose";} 
)

countryListArray has 5 items

In NSLog, I get this(Do Not Want):

countryListArray=(
{Name = "Argentina Australia Brazil Canada Costa Rica";
Capital = "Buenos Aires Canberra Brasilia Ottawa San Jose'";}
)

countryListArray has 1 items

Any ideas on what I'm doing wrong? Yes... I know about the memory leaks, not worried about that right now.

Any help is appreciated.