2012-09-29 61 views
0

我解析XML文件和我的XML看起來像:如何在iphone中解析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 

`我無法得到它..我哪裏錯了?

任何幫助,將不勝感激......

感謝....

回答

0

在你的代碼probem是您要爲相同的鍵添加兩個值。所以第二個值代替第一個值。 您需要添加以下代碼toyour - (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; 
} 
+1

嘿感謝Midhun ....我it..Yahoooo !!!! +100如果可能... – cutiepie

+1

@cutiepie:高興:) –

+0

@Mithun。我有一個小問題,我得到這樣的:({City =「NewYork」; Id =「1」; State = Alabaster;} {如何刪除所有這些({「;從響應和顯示在一個TableView與2行? – cutiepie