-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?

4

1 回答 1

0

请在 viewDidLoad 中初始化您的 iVar

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

因为

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

解析每个元素时都会调用它,因此它会一次又一次地重新分配

于 2012-09-28T10:40:01.163 回答