Reputation: 6384
I am trying to store some xml data into a dictionary but for some reason when I run through the methods for my XMLParser the array and dictionary data that I am trying to set doesn't get set. The XML data is there, I can log it and see the elementName and the stringValues but I can't seem to plug them into an array or dictionary. Not sure what I am doing wrong.
Here's my .h file:
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet id txtSpeechBox;
IBOutlet id btnSpeechBtn;
NSSpeechSynthesizer* synth;
NSURL* urlToPass;
NSXMLParser* dataParser;
NSMutableDictionary* dataDict;
NSMutableArray* dataKeys;
NSString* currentKey;
NSMutableString* currentStringValue;
}
- (void) parseXMLFile : (NSURL *) url;
- (void) speakJasper;
and my .m file:
#import "AppController.h"
@implementation AppController
- (void) awakeFromNib {
//set up our speech synth
synth = [[NSSpeechSynthesizer alloc] init];
//go and grab the weather data
urlToPass = [[NSURL alloc] initWithString:@"http://www.weather.gov/xml/current_obs/KPTW.xml"];
NSData *data = [NSData dataWithContentsOfURL: urlToPass];
NSString *weatherData = [[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding];
[self parseXMLFile:urlToPass];
//NSLog(weatherData);
dataDict = [[NSMutableDictionary alloc] init];
dataKeys = [[NSMutableArray alloc] init];
[dataKeys addObject:@"test line"];
currentKey = [[NSString alloc] init];
[dataDict, dataKeys release];
}
- (void) parseXMLFile : (NSURL *) url {
BOOL success;
if (dataParser) // addressParser is an NSXMLParser instance variable
[dataParser release];
dataParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[dataParser setDelegate:self];
[dataParser setShouldResolveExternalEntities:YES];
success = [dataParser parse]; // return value not used
// if not successful, delegate is informed of error
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentKey = nil;
[currentStringValue release];
currentStringValue = nil;
currentKey = [NSString stringWithFormat: @"%@", elementName];
return;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentStringValue){
currentStringValue = [[NSMutableString alloc] init];
}
[currentStringValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
[dataKeys addObject:currentKey];
[dataDict setObject:currentStringValue forKey:currentKey];
[dataKeys addObject:@"test"];
[self speakJasper];
}
- (void) speakJasper {
//set up a string for what jasper will say
NSMutableString* speakString = [[NSMutableString alloc] init];
speakString = @"Good morning, the current weather is";
//start looping through the dataKeys array to get the dataDict keys, take that value and add to speak string
for(int d=0; d<dataKeys.count; d++) {
NSString* thisDataKey = [dataKeys objectAtIndex:d];
if (thisDataKey == @"weather") {
NSString* thisDataValue = [dataDict objectForKey:thisDataKey];
[speakString appendString:thisDataValue];
}
}
NSLog(@"%i", dataKeys.count);
//[synth startSpeakingString:speakString];
}
@end
Upvotes: 0
Views: 294
Reputation: 512
You're initializing your dictionary and keys AFTER the XML file has already been parsed:
[self parseXMLFile:urlToPass];
dataDict = [[NSMutableDictionary alloc] init];
dataKeys = [[NSMutableArray alloc] init];
so dataDict and dataKeys are both nil as didStartElement:, foundChars: and endElement: are called.
Move that first line below the next two, and you'll get your data.
Upvotes: 1