2012-07-02 44 views
0

我試圖分析一個xml,如果xml提要中有一個空節點,應用程序崩潰。 這可能是什麼原因?解析空節點時,iPhone應用程序崩潰

編輯 我的XML看起來像這樣

<Sponsors> 
    <Sponsor> 
     <Name>name...</Name> 
     <About>blah...blah...blah</About> 
     <Website>http://test.com</Website> 
     <LogoImage>someImage.jpg</LogoImage> 
     <smallIcon>someImage.jpg</smallIcon> 
     <Area/> 
     <BannerImage/>//->>>this node is empty// 
    </Sponsor> 
</Sponsors> 

我使用的解析器NSXML這樣。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
if(nil != self.currentsponsorElement){ 
    [self.currentsponsorElement appendString:string]; 
} 

}

+2

我們需要看到您的XML和用於解析它的代碼。你在檢查錯誤嗎?什麼是錯誤? –

+0

您使用NSXML解析嗎? – Deepesh

+0

@elppa是的,我正在使用NSXML解析 – user1268938

回答

0

不看你的代碼,我只能給你一個普遍的答案 - 這聽起來像你的代碼是「期待」中的一個值「解析器:foundCharacters:」方法。查看你的代碼,看看發現的字符實際上是零時會發生什麼。使用try-catch塊並處理所有可能的場景,這樣你的代碼就不會因其他你還沒有想到的事情而導致崩潰。

1

試試這個: -

內didStartElement使用此代碼: -

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

    if ([elementName isEqualToString:@"Sponsor"]) 
    { 
     if(!soapResults) 
     { 
      soapResults = [[NSMutableString alloc] init]; //declared in .h 
     } 
     recordResults = YES; 
    } 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string { 

    if (string != nil) { 
     [results setString:@""]; 
     [results appendString: string]; 
     NSLog(@"foundResults: %@",results); 
    } 

} 
+0

@ user1268938,查看我的答案,並解決您的問題 – Deepesh

+0

請參閱我的更新回答 – Deepesh

+0

解析器解析nslog中打印的節點的內容,但在最後崩潰。 – user1268938