-1

Iam consuming XML response and parsing it..However when Iam adding the objects to NSMutableDictionary and then to nSMutableArray the response is null.I couldn't understand where iam going wrong? My Xmlresponse is like this:

<List>
<Address>
   <City>Michigan</City>
   <Line1>17</Line1>
   <line2>12</Line2>
 </Address>
<Employee>
    <EmployeeName>Williams</EmployeeName>
 </Employee>
 <Address>
   <City>NewYork</City>
   <Line1>27</Line1>
   <line2>52</Line2>
</Address>
<Employee>
    <EmployeeName>Mandarin</EmployeeName>
</Employee>

My response should be like this:

Michigan 
17
12
Williams

NewYork
27
52
Mandarin

And My Parsing Code is like this:

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

element = elementName;
if([elementName isEqualToString:@"Address"])
    {
        arr=[[NSMutableArray alloc] init];
        drr=[[NSMutableDictionary alloc] init];


    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
 [srr appendString:string];
}


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

if([elementName isEqualToString:@"City"])
{
    [drr setObject:srr forKey:@"City"];
    [srr release],srr=nil;
    return;
}  

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

     [drr setObject:srr forKey:@"Line1"];
     [srr release],srr=nil;
     return;    
}
if([elementName isEqualToString:@"Line2"])
   {

       [drr setObject:srr forKey:@"Line2"];
       [srr release],srr=nil;
       return;      
   }

if([elementName isEqualToString:@"Address"])
{
    [arr addObject:drr];
    [drr release],drr=nil;


}
[srr release],srr=nil;
}
NsLog(@"%@",arr);

But when I check the response at 'arr' it returning NULL?Y is it so?Where Iam I going wrong?

cutiepie
  • 29
  • 1
  • 8
  • hey why are u downvoting?I really do not know the answer.So I have asked...Please help.Im pretty much new to iPhone development and after searching a lot I posted here.I came across some examples but do not know how to apply them in my context.So I have asked ...Please help – cutiepie Sep 28 '12 at 10:22

1 Answers1

0

Please initialize your iVars in viewDidLoad

arr=[[NSMutableArray alloc] init];
        drr=[[NSMutableDictionary alloc] init];

because

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

gets called when each element is parsed,hence it gets reallocated again and and again

AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • As u have said I added them in ViewDidLoad.Now Iam getting error "Program Received Signal SIGABRT" at [drr setObject:srr forKey:@"City"];Y could it be? – cutiepie Sep 28 '12 at 10:57