可可:XML 解析
我试图将一些 xml 数据存储到字典中,但由于某种原因,当我运行 XMLParser 的方法时,我尝试设置的数组和字典数据未设置。 XML 数据就在那里,我可以记录它并查看 elementName 和 stringValues,但我似乎无法将它们插入数组或字典中。不确定我做错了什么。
这是我的 .h 文件:
#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;
和我的 .m 文件:
#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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在解析 XML 文件后初始化字典和键:
因此 dataDict 和 dataKeys 都是 nil,如 didStartElement:、foundChars: 和 endElement: 被调用。
将第一行移到接下来两行下方,您将获得数据。
You're initializing your dictionary and keys AFTER the XML file has already been parsed:
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.