gpt4 book ai didi

xml - cocoa :XML 解析

转载 作者:行者123 更新时间:2023-12-03 18:00:09 24 4
gpt4 key购买 nike

我试图将一些 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

最佳答案

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

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

因此,在调用 didStartElement:foundChars:endElement: 时,dataDict 和 dataKeys 均为 nil。

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

关于xml - cocoa :XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com