我正在解析一个 XML 文件,我的 XML 看起来像:
<List>
<CompanyallProvidersList>
<CityDetails>
<City>NewYork</City>
<Id>1</Id>
</CityDetails>
<StateDetails>
<State>Alabaster</State>
</StateDetails>
<CityDetails>
<City>Newyork</City>
<Id>1</Id>
</CityDetails>
<StateDetails>
<State>California</State>
</StateDetails>
</CompanyallProvidersList>
</List>
我的解析器代码是这样的:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"CompanyallProvidersList"])
{
arr=[[NSMutableArray alloc] init];
drr=[[NSMutableDictionary alloc] init];
}
srr=[[NSMutableString 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:@"Id"])
{
[drr setObject:srr forKey:@"Id"];
[srr release],srr=nil;
return;
}
if([elementName isEqualToString:@"State"])
{
[drr setObject:srr forKey:@"State"];
[srr release],srr=nil;
return;
}
if([elementName isEqualToString:@"CompanyallProvidersList"])
{
[arr addObject:drr];
[drr release],drr=nil;
}
[srr release],srr=nil;
}
我的输出是这样的:
NewYork
1
California
但我希望输出为:
NewYork
1
Alabaster
NewYork
1
California
`我无法得到它..我哪里错了?
任何帮助,将不胜感激......
谢谢....