简体   繁体   中英

iPhone app crashes when empty node is parsed

I'm trying to parse an xml and the app crashes if there is an empty node in the xml feed. What may be the reason for this?

EDIT my xml looks like this

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

I'm using NSXML Parser like this.

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

}

Try This:-

inside didStartElement use this code:-

- (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);
    }

}

Without looking at your code, I can only give you a general answer - it sounds like your code is "expecting" a value in the "parser:foundCharacters:" method. Go over your code and see what happens when the characters found are in fact nil. Use try-catch blocks and handle all possible scenarios so your code doen't cause crashes as a result of other things you still haven't thought of.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM