可可:XML 解析

发布于 2024-12-11 05:40:42 字数 3256 浏览 0 评论 0原文

我试图将一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-12-18 05:40:42

您正在解析 XML 文件后初始化字典和键:

[self parseXMLFile:urlToPass];
dataDict = [[NSMutableDictionary alloc] init];
dataKeys = [[NSMutableArray alloc] init];

因此 dataDict 和 dataKeys 都是 nil,如 didStartElement:foundChars:endElement: 被调用。

将第一行移到接下来两行下方,您将获得数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文