0

我正在解析一个 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

`我无法得到它..我哪里错了?

任何帮助,将不胜感激......

谢谢....

4

1 回答 1

0

在您的代码问题中,您正在为相同的键添加两个值。所以第二个值替换了第一个值。您需要将以下代码添加到您的- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName委托

if([elementName isEqualToString:@"StateDetails"])
{
  [arr addObject:drr];
}

并改变:

if([elementName isEqualToString:@"CompanyallProvidersList"])
{
[drr release];
}

还将以下代码添加到您的- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

if([elementName isEqualToString:@"CityDetails"])
{
   drr=[[NSMutableDictionary alloc] init];
}

它会工作的!

现在代码将如下所示:

- (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];
  }
  if([elementName isEqualToString:@"CityDetails"])
  {
     drr=[[NSMutableDictionary alloc] init];
  }
  srr=[[NSMutableString alloc] init];
}

- (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:@"StateDetails"])
   {
     [arr addObject:drr];
   }

  if([elementName isEqualToString:@"CompanyallProvidersList"])
  {
     [drr release],drr=nil;
  }
  [srr release],srr=nil;
}
于 2012-09-29T11:29:15.383 回答