I'm using NSXMLParser to parse a small XML file containing information about videos. One of the elements in the XML is a URL that points to a thumbnail image associated with each video. I'm trying to parse the XML, get the ImageURL and then use this to fetch the image and store it along with strings such as the video name.
I just can't seem to get it to work. I can see from the debugger that the dictionary has the key/value pair for the image and the image is getting created from the URL, but I'm failing to add it to the NSDictionary. Any suggestions? The following is the fragment of code that deals with the parsing.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"clipid"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentClipID = [[NSMutableString alloc] init];
currentClipName = [[NSMutableString alloc] init];
currentImageURL = [[NSMutableString alloc] init];
currentImage = [[UIImage alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"clipid"]) {
[currentClipID appendString:string];
} else if ([currentElement isEqualToString:@"clipname"]) {
[currentClipName appendString:string];
} else if ([currentElement isEqualToString:@"imageurl"]) {
[currentImageURL appendString:string];
//Take the Image URL, and convert it to an Image
NSURL *imageURL = [NSURL URLWithString:string];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
currentImage = //If it were a string I'd use appendString. How do I add this image to the Dictionary?
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"clipid"]) {
// save values to an item, then store that item into the array...
[item setObject:currentClipID forKey:@"clipid"];
[item setObject:currentClipName forKey:@"clipname"];
[item setObject:currentImageURL forKey:@"imageurl"];
[item setObject:currentImage forKey:@"image"];
//videos is an NSMutableArray
[videos addObject:[item copy]];
}
}