所有元素都存储在 NSStrings 中,除了 dog 存储在 NSArray 中。我希望将所有狗条目添加到 NSArray。现在只有一个狗值被添加到数组中。如何将所有狗值添加到 NSArray?
<root>
<element>
<person>Jim<person/>
<age>21<age/>
<dog>Labrador<dog/>
<dog>Beagle<dog/>
<dog>Yorkshire Terrier<dog/>
<element/>
<element>
<person>Jill<person/>
<age>50<age/>
<dog>Boxer<dog/>
<dog>Pug<dog/>
<dog>Labrador<dog/>
<dog>Beagle<dog/>
<dog>Yorkshire Terrier<dog/>
<element/>
</root>
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"root"]) {
//Initialize the array.
appDelegate.entries = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"element"]) {
//Initialize the entry.
anEntry = [[Entry alloc] init];
//Extract the attribute here.
anEntry.entryID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", anEntry.entryID);
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"root"])
return;
if([elementName isEqualToString:@"element"]) {
[appDelegate.entries addObject:anEntry];
anEntry = nil;
}
else
[anEntry setValue:currentElementValue forKey:elementName];
currentElementValue = nil;
}