2
所有元素都存儲在NSStrings中,除了存儲在NSArray中的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;
}