parsing and displaying in tableview

Discussion in 'iPhone/iPad Programming' started by jennylcf, Jan 27, 2013.

  1. jennylcf, Jan 27, 2013
    Last edited by a moderator: Jan 27, 2013

    macrumors newbie

    Joined:
    Jan 27, 2013
    #1
    I'm having a hard time displaying my xml in my tableview. I parse it successfully using NSXMLparser but when i am retrieving the data and trying display it on my table view the output is not complete. I have 3 tableviews, 1 is for the list of countries the 2nd is for the list of <subsidiary> the 3rd is for the information. The problem occurs when 1 country has 2 or more <subsidiary>, it only display the last subsidiary of that country. here is my xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <countries>
        <country>
                <countryname>Philippines</countryname>
                <subsidiaries>
                        <subsidiary>
                                <name id = "1">Sartorius Philippines Inc.</name>
                                <address>Unit 20-A The World Centre Building, 330 Senator Gil Puyat Avenue Makati 1209 City Philippines, Philippines</address>
                                <phone>+63.2.8640.929</phone>
                                <fax>+63.28640.932</fax>
                                <email>enquiry.philippines@sartorius.com</email>
                                <website>http://www.sartorius-mechatronics.com.ph</website>
                        </subsidiary>
                </subsidiaries>
        </country>
        <country>
            <countryname>Denmark</countryname>
                <subsidiaries>
                    <subsidiary>
                            <name>Sartorius Stedim Nordic A|S</name>
                            <address>stedim Hoerskaetten 6d 2630 Taastrup, Denmark</address>
                            <phone>+45.7023.4400</phone>
                            <fax>+45.4630.4030</fax>
                            <email>ne.customersupport@sartorius.com</email>
                            <website></website>
                    </subsidiary>
                    <subsidiary>
                            <name>Sartorius Nordic A|S</name>
                            <address>Hoerskaetten 6D 2630 Taastrup, Denmark</address>
                            <phone>+45.7023.4400</phone>
                            <fax>+45.4630.4030</fax>
                            <email>ne.customersupport@sartorius.com</email>
                            <website></website>
                    </subsidiary>
                </subsidiaries>
        </country>
    </countries>
    as you can see the <countryname> Denmark has two <subsidiary> in it. can you help me display all the subsidiary in that country?

    here is my parser class, DON'T MIND THE app.infoarray,



    Code:
    #import "Parser.h"
    
    @implementation Parser
    
    
    -(id)initParser
    {
        if (self == [super init])
        {
            app = (AppDelegate *) [[UIApplication sharedApplication]delegate];
        }
    
        return self;
    }
    
    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementName isEqualToString:@"countries"])
        {
               app.listArray = [[NSMutableArray alloc]init];
               app.infoarray = [[NSMutableArray alloc]init];
        }
      if ([elementName isEqualToString:@"country"])
        {
            theList = [[List alloc]init];
            temp = [[NSMutableString alloc]init];
        }
        if ([elementName isEqualToString:@"subsidiary"]) {
            theList.countryname = temp;
            [app.infoarray addObject:theList.countryname];
        }
       
    }
    
    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            
    }
    
    
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        if ([elementName isEqualToString:@"subsidiary"])
        {
            [app.listArray addObject:theList];
        }
        else
        {
             if ([elementName isEqualToString:@"countryname"])
             {
                 temp = currentElementValue;
              
             }
            if ([elementName isEqualToString:@"name"])
            {
                
                theList.name = currentElementValue;
              
             
            }
            if ([elementName isEqualToString:@"address"])
            {
                theList.address = currentElementValue;
               
            }
            if ([elementName isEqualToString:@"phone"])
            {
                theList.phone = currentElementValue;
                
            }
            if ([elementName isEqualToString:@"fax"])
            {
                theList.fax = currentElementValue;
              
            }
            if ([elementName isEqualToString:@"email"])
            {
                theList.email = currentElementValue;
               
            }
            if ([elementName isEqualToString:@"website"])
            {
                theList.website = currentElementValue;
                
            }
            
           currentElementValue = nil;
           
        }
    
    }
    
    @end
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I can't see how your subsidiary information gets associated with a country at all... It looks like all of the subsidiary information goes in a single app.listarray?

    How do you know your issue is with what you've shown us and not your Table View Controller?
     
  3. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #3
    There is no association between the subsidiary and the country. He would have to create a country object that can contain and array of subsidiary objects, or he would have to make the entire list only subsidiary objects and add a country property to each subsidiary as he creates the new instances to add them to the array. This would allow him to filter the array based on country and only get the elements where the country property is the country he is looking for. He can also get a list from the subsidiaries of all unique countries to populate the list from one set of data.
     

Share This Page