How to add default value to array when child is not found in xml parsing?

I am new to iphone development. I parse an XML page and display the contents in a table. In some block on the XML page there is no child element, I want to add the value o to my array when the child element is not like in the specific block. My xml file is similar to

 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 <gd:ratings numRaters="2"/>
 </entry> ..................................>first block
 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 </entry> ....................................>Second block
 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 <gd:ratings numRaters="2"/>
 </entry> .....................................>Third block

I parse the gd: ratings tag and display its numRates value in the table. I have a mutable array that stores the numRates value. I need to add the object "O" to the array when the second block is parsed because it does not contain the gd: ratings tag. Since the value is present in the attribute tag, to obtain content and add it to the variable array is performed in the NSXML DidStartElement parser method.

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

currentElement = [elementName copy];
if ([elementName isEqualToString:@"entry"]) {

    entry = [[NSMutableDictionary alloc] init];
    currentTitle = [[NSMutableString alloc] init];
    currentDate = [[NSMutableString alloc] init];
    NSLog(@"inside image1 ");
}else if([elementName isEqualToString:@"media:thumbnail"])
    { currentImage = [[NSMutableString alloc] init];
        if(myUrl == nil){   
        myUrl = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
        }
        [currentImage appendString:myUrl];  
        [stories1 addObject:currentImage];
        myUrl=nil;
        }   else if([elementName isEqualToString:@"gd:rating"])
{
    currentRating=[[NSMutableString alloc]init];

        myRatings=[NSString stringWithString:[attributeDict objectForKey:@"numRaters"]];

    [currentRating appendString:myRatings];
    [stories2 addObject:currentRating];
}   
}

25 , 10 gd. array2 15 . tableview . - " ". , . .

+3
2

didEndElement: , , .

iVar BOOL entryHasRating :

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

   currentElement = [elementName copy];
   if ([elementName isEqualToString:@"entry"]) {
     entryHasRating = NO;
     ...
   } else if ([elementName isEqualToString:@"gd:rating"]){
     entryHasRating = YES;
     ...
   }
}

, :

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
   if ([elementName isEqualToString:@"entry"]&& !entryHasRating) {
       [stories2 addObject:@"No Rating"];
       entryHasRating = YES; 
   }
   ...
}
+2

, , XML; XML .

+1

Source: https://habr.com/ru/post/1732026/


All Articles