Search code examples
iphoneiosobjective-cnsxmlparser

How to retrieve country name from xmlParser


I am trying to retrieve countries name from a xml. Here is the xml result.

<Table diffgr:id="Table1" msdata:rowOrder="0"><id>1</id><country>Afghanistan</country><isd_code>93</isd_code><timezone>+04:30</timezone><visible>false</visible></Table><Table diffgr:id="Table2" msdata:rowOrder="1"><id>2</id><country>Albania</country><isd_code>355</isd_code><timezone>+01:00</timezone><visible>false</visible></Table>

In parser did end element method

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

    if ( [elementName isEqualToString:@"Table"] ) {
       NSLog(@"attribute is %@",attributeDict);
    }
}

OutPut is

attribute is {
    "diffgr:id" = Table1;
    "msdata:rowOrder" = 0;
}

How can i retrieve country name. Thanks in advance.


Solution

  • Try this, in viewDidLoad:-

    countryarr=[[NSMutableArray alloc]init];
    nodeContent=[[NSMutableString alloc]init];
    
    
     - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
    [nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    
    } 
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    
    {
         if ([elementName isEqualToString:@"country"])
    {
        [countryarr addObject:nodeContent];
    }
     nodeContent=[[NSMutableString alloc]init];
    
    }