Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6740537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:37:04+00:00 2026-05-26T11:37:04+00:00

I am trying to store some xml data into a dictionary but for some

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:37:04+00:00Added an answer on May 26, 2026 at 11:37 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to design some tables to store some data, which has to be
I'm trying to store an xml serialized object in a cookie, but i get
I have some XML data retrieved from a web service that I use to
I'm trying to retrieve an webpage that has XML data using file_get_contents(). $get_url_report =
I have some XML that stores column information and row data from a table
Im trying to persist some data but im getting an error here. Declaration of
I need to download and parse some XML data, and to store it in
I am trying to render some data from a large XML using XSLT. The
I'm trying to scrape some tables (election data) using the XML package. Browsing SO,
I'm trying to extract monetary sums stored in some poorly formated xml columns (there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.