Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-27-2010, 01:58 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 14
jelkimantis is on a distinguished road
Default NSDictionary XML

Here is what I am trying to do. Please help me understand what I'm doing wrong!

I have an XML file that I want to parse. I want to be able to parse this somewhat dynamically, given that we'll be using it for various archives of the same format. Given this idea, I want to put each tag into an NSDictionary.

Since I'm parsing XML, I need to build the results, which I am doing like this:

Code:
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	[results appendString:string];
}
When the element ends, I have an if-else chain that assigns results to the appropriate NSDictionary:

Code:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
	qualifiedName:(NSString *)qName {
	if (currentProperty) {
		//set the idTag for the entry and appent the idTag to the array of Entries.
		if ([currentProperty isEqualToString: @"Thumb"]) {
			[thumbURL setObject:results forKey:idTag];
			[results setString:@""];
		} else if ([currentProperty isEqualToString: @"Path"]) {
			[pathURL setObject:results forKey:idTag];
			[results setString:@""];
		} else if ([currentProperty isEqualToString: @"Transcription"]) {
			[transcription setObject:results forKey:idTag];
			[results setString:@""];
		} else if ([currentProperty isEqualToString: @"Meta"]) {
			//Haven't written this yet.
			[results setString: @"" ];
		} else if ([currentProperty isEqualToString: @"Context"]) {
			[context setObject:results forKey:idTag];
			[results setString:@""];
		} else if ([currentProperty isEqualToString: @"Inferences"]) {
			[inferences setObject:results forKey:idTag];
			[results setString:@""];
		}
	}
	if ([currentProperty isEqualToString:@"Entry"]) {
		[meta setObject:individualMeta forKey:idTag];
		[individualMeta removeAllObjects];
	}
	//reset current properties
	currentProperty = nil;
	attributeValue = nil;
}
The problem seems to be that when I set my dictionary entries, it's maintaining a persistent connection to 'results'. I tested this theory by putting:
Code:
NSLog(@"transcription %@", [transcription objectForKey:idTag]);
both before and after the
Code:
[results setString:@""];
before that line of code, it returns what I want, after, it returns nothing.

I have a sneaking suspicion that I'm just overlooking some detail, but any help would be greatly appreciated. Let me know if you need any other information about my code.
jelkimantis is offline   Reply With Quote
Old 05-27-2010, 02:12 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 14
jelkimantis is on a distinguished road
Default

Don't you love when you fix your own code before anyone else does?

So, since I knew that I was just referencing objects, I decided to try to copy them into my dictionary.

so I changed:
Code:
[thumbURL setObject:results forKey:idTag];
to
Code:
[thumbURL setObject:[results copy] forKey:idTag];
And now it works like a top! :-) I have a feeling that this app will get refactored pretty heavily when I get it working, but that's life, right? Thanks for being here! :-)

If this solution is lame and absolutely moronic, please let me know, otherwise, I hope this helps someone else! :-)
jelkimantis is offline   Reply With Quote
Old 12-15-2010, 02:53 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 5
Benoit Caccinolo is on a distinguished road
Default easily convert XML to NSDictionary

To easily convert XML to NSDictionary, here is a little lib to do this conversion:

XML to NSDictionary Converter bcaccinolo's blog

The code is on Github.

Cheers,
Benoit Caccinolo is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, nsdictionary, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
10 members and 307 guests
ajay123123, ashaman64, baja_yu, ChrisYates, guusleijsten, HemiMG, newDev, Objective Zero, pkIDSF, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0