For the past year or so, I’ve been very lucky. All the data I’ve had to deal with has been packaged in JSON, not XML. And what a glorious year it’s been. Instead of writing complex, single-use XML-parsing code, I’ve had the joy of using Stig Brautaset’s excellent JSON-framework to parse JSON. The framework is dead simple to use. Have some JSON? Throw some JSON at the framework, and viola! You get back an NSDictionary or an NSArray. Just one line of code, and you’re done. Simple, elegant, and completely opposite to the experience of parsing XML.
What’s the problem parsing XML? Well, first you have to set up your NSXMLParser. Then, make sure you’re set as the delegate. Then, override the necessary delegate methods (there are 14 relevant NSXMLParserDelegate methods, so choose wisely!). Then, initialize your NSMutableString to record the strings from the text nodes. Then, initialize your model objects from the XML as elements are pushed and popped in the didStart and didEnd methods. And don’t forget to update your objects with data in the attributes dictionary. And so on, and so on.
XML files, especially small XML files, should be as easy to parse as JSON.
Last week, when faced with the depressing task of writing my first XML parser this year, I wrote a general-purpose XML to NSDictionary parser instead. Throw some XML at it, and it spits out an NSDictionary.
How does it work? Here are the key ideas:
- XML elements map to key names in the dictionary
- Each element corresponds to a child dictionary
- Attribute key-value pairs are added to the element’s child dictionary
- Strings from text nodes are assigned to the child dictionary’s “text” key
- If an element name is encountered multiple times, the value of the element is set to an array of children dictionaries
This conversion is not without its flaws, but it should work pretty well for most XML files.
The Code
The parser consists of a single class, XMLReader. You can either pass it an XML string or an XML data object, and it will return the NSDictionary version of the XML. If the XML is malformed or the parser fails for any other reason, the NSError pointer you pass in will be populated with an NSError object.
Here’s the header file:
// XMLReader.h
//
#import <Foundation/Foundation.h>
@interface XMLReader : NSObject
{
NSMutableArray *dictionaryStack;
NSMutableString *textInProgress;
NSError **errorPointer;
}
+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer;
+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer;
@end
And the implementation:
// XMLReader.m
//
#import "XMLReader.h"
NSString *const kXMLReaderTextNodeKey = @"text";
@interface XMLReader (Internal)
- (id)initWithError:(NSError **)error;
- (NSDictionary *)objectWithData:(NSData *)data;
@end
@implementation XMLReader
#pragma mark -
#pragma mark Public methods
+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error
{
XMLReader *reader = [[XMLReader alloc] initWithError:error];
NSDictionary *rootDictionary = [reader objectWithData:data];
[reader release];
return rootDictionary;
}
+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [XMLReader dictionaryForXMLData:data error:error];
}
#pragma mark -
#pragma mark Parsing
- (id)initWithError:(NSError **)error
{
if (self = [super init])
{
errorPointer = error;
}
return self;
}
- (void)dealloc
{
[dictionaryStack release];
[textInProgress release];
[super dealloc];
}
- (NSDictionary *)objectWithData:(NSData *)data
{
// Clear out any old data
[dictionaryStack release];
[textInProgress release];
dictionaryStack = [[NSMutableArray alloc] init];
textInProgress = [[NSMutableString alloc] init];
// Initialize the stack with a fresh dictionary
[dictionaryStack addObject:[NSMutableDictionary dictionary]];
// Parse the XML
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
BOOL success = [parser parse];
// Return the stack’s root dictionary on success
if (success)
{
NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
return resultDict;
}
return nil;
}
#pragma mark -
#pragma mark NSXMLParserDelegate methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// Get the dictionary for the current level in the stack
NSMutableDictionary *parentDict = [dictionaryStack lastObject];
// Create the child dictionary for the new element, and initilaize it with the attributes
NSMutableDictionary *childDict = [NSMutableDictionary dictionary];
[childDict addEntriesFromDictionary:attributeDict];
// If there’s already an item for this key, it means we need to create an array
id existingValue = [parentDict objectForKey:elementName];
if (existingValue)
{
NSMutableArray *array = nil;
if ([existingValue isKindOfClass:[NSMutableArray class]])
{
// The array exists, so use it
array = (NSMutableArray *) existingValue;
}
else
{
// Create an array if it doesn’t exist
array = [NSMutableArray array];
[array addObject:existingValue];
// Replace the child dictionary with an array of children dictionaries
[parentDict setObject:array forKey:elementName];
}
// Add the new child dictionary to the array
[array addObject:childDict];
}
else
{
// No existing value, so update the dictionary
[parentDict setObject:childDict forKey:elementName];
}
// Update the stack
[dictionaryStack addObject:childDict];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// Update the parent dict with text info
NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];
// Set the text property
if ([textInProgress length] > 0)
{
// Get rid of leading + trailing whitespace
[dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
// Reset the text
[textInProgress release];
textInProgress = [[NSMutableString alloc] init];
}
// Pop the current dict
[dictionaryStack removeLastObject];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// Build the text value
[textInProgress appendString:string];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
// Set the error pointer to the parser’s error object
*errorPointer = parseError;
}
@end
The code works by keeping a stack of dictionaries, one for each level of the XML file. Each time a new tag is encountered, a child dictionary is pushed onto the stack. Each time a tag is closed, the dictionary is popped off the stack.
Arrays of elements are detected when the same key appears twice in the dictionary. For instance, if the XML is “<book><page>1</page><page>2</page></book>”, the first time the “page” element is encountered, a child dictionary will be set as the value for the “page” key. The next time the “page” element is encountered, we detect that there’s already a value for the “page” key, so we put both pages in an array and set the value of the “page” key to the array.
Note: One side effect of detecting arrays in this fashion is that the value for a key (say “page” in the example above), can sometimes be set to an NSArray but other times be set to an NSDictionary. For example, if the book contains a single page, “page” will be set to an NSDictionary. If the book contains 2 or more pages, “page” will be set to an NSArray. You will need to account for this when reading from the dictionary produced by dictionaryFromXMLString:error: and dictionaryFromXMLData:error:.
When the parser comes across a text node in the XML, it inserts a key into the dictionary named “text” and sets the value to the parsed string.
Note: Make sure the XML you’re parsing doesn’t contain a field named “text”! You can change it to a non-conflicting name by editing the kXMLReaderTextNodeKey constant at the top of XMLReader.m.
Using the Code
The conversion is best illustrated by an example. The snippet below defines an XML string that is converted into a dictionary using XMLReader:
// XML string from http://labs.adobe.com/technologies/spry/samples/data_region/NestedXMLDataSample.html
//
NSString *testXMLString = @"<items><item id=\"0001\" type=\"donut\"><name>Cake</name><ppu>0.55</ppu><batters><batter id=\"1001\">Regular</batter><batter id=\"1002\">Chocolate</batter><batter id=\"1003\">Blueberry</batter></batters><topping id=\"5001\">None</topping><topping id=\"5002\">Glazed</topping><topping id=\"5005\">Sugar</topping></item></items>";
// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
// Print the dictionary
NSLog(@"%@", xmlDictionary);
//
// testXMLString =
// <items>
// <item id=”0001″ type=”donut”>
// <name>Cake</name>
// <ppu>0.55</ppu>
// <batters>
// <batter id=”1001″>Regular</batter>
// <batter id=”1002″>Chocolate</batter>
// <batter id=”1003″>Blueberry</batter>
// </batters>
// <topping id=”5001″>None</topping>
// <topping id=”5002″>Glazed</topping>
// <topping id=”5005″>Sugar</topping>
// </item>
// </items>
//
// is converted into
//
// xmlDictionary = {
// items = {
// item = {
// id = 0001;
// type = donut;
// name = {
// text = Cake;
// };
// ppu = {
// text = 0.55;
// };
// batters = {
// batter = (
// {
// id = 1001;
// text = Regular;
// },
// {
// id = 1002;
// text = Chocolate;
// },
// {
// id = 1003;
// text = Blueberry;
// }
// );
// };
// topping = (
// {
// id = 5001;
// text = None;
// },
// {
// id = 5002;
// text = Glazed;
// },
// {
// id = 5005;
// text = Sugar;
// }
// );
// };
// };
// }
//
The mapping between XML and NSDictionary is shown in the comments above. Notice how the “batter” and “topping” keys are set to arrays since there were multiple “batter” and “topping” keys in the XML. Also, note how the attributes for elements are available in the element’s child dictionary. For instance, the “id” and “type” attributes for item “item” are keys in the “item” dictionary.
Download
You can download the XMLReader files here:
Done and Done
XMLReader isn’t perfect by any stretch of the imagination, but hopefully it helps save you some pain and misery next time you need to parse an XML file in Objective-C.
Awesome! Thanks for releasing this, makes me watt to write and share cool code. I’ve used your site a few times, you have some of the best stuff regarding in app purchase as well. Very exhaustive and informative. Looking forward to testing this out. Thanks again!
–nick
Somehow I can’t find the link to this code posted on Github, that would make sense, instead of a Zip file. People could contribute and all that
Just about the only thing that could make this cooler would be:
+ (NSDictionary *)dictionaryForXMLURL:(NSURL *)url error:(NSError **)errorPointer;
Would make grabbing RSS feeds, or just about any other XML-based web content that much more streamlined
Could you please tell me some dictionary methods to read the dictionary you are generating.
My dictionary generated is:
Menus = {
Table1 = {
Phone = {
text = “\n (404) 371-1466″;
};
“Restaurant_Name” = {
text = “\n \n CHICK-FIL-A”;
};
“Restaurant_id” = {
text = “\n 2748″;
};
“billing_addr1″ = {
text = “\n 105 EAST TRINITY PLACE”;
};
“billing_city” = {
text = “\n DECATUR”;
};
“billing_state” = {
text = “\n GA”;
};
“billing_zip_code” = {
text = “\n 30030″;
};
text = “\n “;
};
text = “\n”;
};
}
Now how to read Restaurant_Name from this dictionary?
+1 on the github suggestion!
@Abhi
Say your dictionary is named xmlDictionary, then you would get the Restaurant_Name value like this:
NSString *restaurantName = [[[[xmlDictionary objectForKey:@"Menus"] objectForKey:@”Table1″] objectForKey:@”Restaurant_Name”] stringForKey:@”text”];
Also, if you want to get rid of the leading and trailing whitespace, try this:
restaurantName = [restaurantName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Thanks Troy, but that leading and trailing white space not working for me:
I have a string like this: myString =
(
“\n \n 24 K CLUB”,
“\n \n 3 DOLLAR CAFE”,
“\n \n A PEACH OF A PARTY”,
“\n \n A ROYAL AFFAIR CAFE”,
“\n \n AFFAIRS TO REMEMBER CATERERS”,
“\n \n AFRIKAN DELI”)
How to remove those new characters and white spaces
You are just so nice to share this wonderful article. I have personally experienced in parsing around 20 xml files and I knew that what I did was a repeated job, have not thought of a solution in generic like yours. Cheers!!!
Brilliant work, thank you so much!
Thank you so much!
If I may I would like to suggest not putting the “text” element in the dictionary, it’s one more value to navigate
Great, really really great work!
Please, put the code in github !!
Nice piece of code, but wouldn’t it just make more sense to put values into arrays with 1 entry instead of NSDictionary and use NSDictionary only for attributes? I really hate to have to check what kind of object came back every time I am not sure about it…
Alex
Guys! Now you can fork and have fun
https://github.com/bcaccinolo/XML-to-NSDictionary
Thanks for this great piece of code Troy !
I don’t seem to be able to get this to work when passing an NSData object.. it returns ‘null’ to the console.
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:responseData error:&parseError];
Any ideas?
I have wrote something just like yours but I am a fresh of obj-c, and my classes cause memory leaks, so thanks to your work, i will try it :p
Thanks for posting this. I too had a “Ugh. Why isn’t this JSON?” moment tonight. I’m glad I found your snippet!
@Abhi
I was having the same issue as you, I’m trying something that might work but have yet to get to using the created dictionary object, i’ll let you know if i run into any issues (if i don’t reply again, obviously no issues)
In the XMLReader.m file change the foundCharacters: function to — [textInProgress appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; stripping out the whitespace and newline characters before it’s added to the dictonary item.
Hi, this class was amazingly helpful. Thanks. I’ve made a couple of small changes that make it a bit easier to work with. In my situation, my xml doesn’t use attributes; rather the tag values are all there is. I’ve changed the class as follows:
1. Added a public method:
+ (NSDictionary *)dictionaryForPath:(NSString *)path error:(NSError **)errorPointer;
2. Got rid of some memory leaks that build&analyse spotted.
3. When there are no attributes for a tag, but there is a “text” value, to replace the single entry dictionary with a simple string object containing the tag value. This makes it so much easier to get string tag values without having to negotiate dictionaries.
I was going to replace the single item dictionaries with arrays for consistency, but decided it was actually handy to be able to use the iskindofclass() to quickly determine whether it would be an array or a single item dictionary.
I’ve forked in the repository and delivered my changes.
Thanks for posting this.Your generic method for solving the hectic problem of xml parsing was truly helpful.Now am having another problem..I am developing an application based on a survey,which includes matrix multiple choice questions for which i am not having any idea about implementing it.i am sure you will help me on this…waiting for your valuable response..Thanks in advance..
this utility worked great. it converted my xml to a dictionary. now i want to break up the dictionary into separate arrays….or is it more common to break it up into sub dictionaries? essentially i want to take chunks of the dictionary so that i can create a grid of UIViews or UIImages.
so this works fine for all the keys and values:
for (id key in xmlDictionary) {
NSLog(@”key: %@, value: %@”, key, [xmlDictionary objectForKey:key]);
}
here is a portion of the dictionary that i wrote to the console:
section = (
{
name = running;
pic = “main_running.jpg”;
text = “\n\t\n\t\tmain running blah t”;
},
{
name = “women’s training”;
pic = “main_womentrain.jpg”;
text = ” \n\t\tmain women train txt”;
},
as an example how do i create a array of the “pic” keys in this dictionary? from there i’m hoping i can create my grid of images.
thanks for this utility. it works great and i think i’m close to figuring out how to manipulate a dictionary,
josh
Mate how do i get this reading a XML from the web?
Hey,
I tried your code and it works great however Im having problems displaying the data in a table view. It’s a little trickier to populate a TableView with a dictionary than an nsarray.
Any pointers on that?
Latest fork from github is providing a slightly nicer dictionary result for strings:
https://github.com/Insert-Witty-Name/XML-to-NSDictionary
Many improvements were done (memory leaks, strings, paths, …).
iam a newbie in ios programming.I need your help.
I have an XMl file which i parsed using NSXML parser but the issue iam facing is i cant store or access the attributes using NSMutableArray.
can u help me in writing a code for the below XML for extracting the values and store in an NSMutable array. And also i have an issue in my parser that is my function [attributeDict objectForKey:key] is returning null.(i used the in place of key @”VehicleName”)
demof
OR 02AT-2110
Stop
NA
0
0 hrs 46 min 5sec
Nandan Kanan Rd, Chandrasekharpur, Bhubaneswar, Orissa, India
OR 02AT-2110
9937291959
2011/06/09 15:57:27
20.349966,85.825363
TN 18B-0865(TM 10)
Stop
NA
0
5 hrs 6 min 38sec
State Highway 113, Thandalam, Tamil Nadu, India
TN 18A-0865
9677127842
2011/06/09 15:58:42
13.005752,80.0931
TN 18A-7204 (BK)
Stop
NA
0
1 hrs 55 min 14sec
State Highway 27, Tamil Nadu, India
TN 18A-7204
9677128001
2011/06/09 15:58:50
11.153144,79.063321
This is my xml
}
- (void)parser:(NSXMLParser *)parser didStartElement:
(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"LoginResponse"]&& (currentElement == nil))
{
[window addSubview:rootController.view];
[UIView commitAnimations];
arrayList = [ [NSMutableArray alloc] initWithObjects:@”hello”,vehicleName,nil];
}
currentElement = elementName;
//NSLog(@”%@”,[attributeDict objectForKey :@"LoginResponse"]);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([currentElement isEqualToString :@"VehicleName"])
{
vehicleName = string;
NSLog(@”%@”,vehicleName);
}
// NSLog(@”array values are %@”,[arrayList objectAtIndex:1]);
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@”parsing error……………………………………………..”);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
printf(“Succeeded! Received bytes of data…………………………….\n\n\n\n”);
NSLog(@”%@”,completeData);
NSString *responseString1 = [[NSString alloc] initWithData:completeData encoding:NSISOLatin1StringEncoding];
NSLog(@”%@”,responseString1);
NSLog(@”testing4………”);
NSXMLParser *pXML = [[NSXMLParser alloc] initWithData: completeData];
[pXML setDelegate:self];
[pXML parse];
// release the connection, and the data object
//[connection release];
//[receivedData release];
this is a part of my code and also i got the xml from a webserver
please email help
Thanks for this awesome bit of code, but I do think I’ve found what I think is a bug: it incorrectly parses a “self-closing” tag, such as “”. This results in an NSDictionary entry that has this value:
“yt:assignable” = {
text = “\n \n “;
};
Shouldn’t it just be an empty string?
(Yes, I have the code and can fix it myself, but I figured I should let others know what I found.)
Thanks again!
The downloaded files are commented “Copyright…All Rights Reserved”. Are there licensing terms you want us to adhere to in using this?
- NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
+ NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:data] autorelease];
This is the best simple code I have ever been looking for.
Great! I was up and running in minutes! The only add I did was to put the code into trim whitespace (you had it as a comment). For my data the whitespace was hideous.
Inside “didEndElement:”
// Get rid of leading + trailing whitespace
[dictInProgress setObject:[textInProgress stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:kXMLReaderTextNodeKey];
Now this is great going from XML to NSDictionary, but how difficult would it to go from NSDictionary back to XML producing the same exact XML? I see that it only “reads” XML, but doesn’t “write” to XML.
hello there i was wondering how should do error handling for example if the xml got an error.. the source of xml if fetch from http.. it returns a NSData.. how should i do this thanks in advance for any help