Skip to content

Instantly share code, notes, and snippets.

@romainbriche
Created February 27, 2012 09:07
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainbriche/1922643 to your computer and use it in GitHub Desktop.
Save romainbriche/1922643 to your computer and use it in GitHub Desktop.
Parse XML to NSDictionary using TBXML
#import "TBXML.h"
@interface TBXML (TBXML_NSDictionary)
+ (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element;
+ (NSDictionary*)dictionaryWithXMLData:(NSData*)data;
@end
#import "TBXML+NSDictionary.h"
@implementation TBXML (TBXML_NSDictionary)
+ (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element
{
NSMutableDictionary *elementDict = [[NSMutableDictionary alloc] init];
TBXMLAttribute *attribute = element->firstAttribute;
while (attribute) {
[elementDict setObject:[TBXML attributeValue:attribute] forKey:[TBXML attributeName:attribute]];
attribute = attribute->next;
}
TBXMLElement *childElement = element->firstChild;
if (childElement) {
while (childElement) {
if ([elementDict objectForKey:[TBXML elementName:childElement]] == nil) {
[elementDict addEntriesFromDictionary:[self dictionaryWithXMLNode:childElement]];
} else if ([[elementDict objectForKey:[TBXML elementName:childElement]] isKindOfClass:[NSArray class]]) {
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:[elementDict objectForKey:[TBXML elementName:childElement]]];
[items addObject:[[self dictionaryWithXMLNode:childElement] objectForKey:[TBXML elementName:childElement]]];
[elementDict setObject:[NSArray arrayWithArray:items] forKey:[TBXML elementName:childElement]];
[items release]; items = nil;
} else {
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[elementDict objectForKey:[TBXML elementName:childElement]]];
[items addObject:[[self dictionaryWithXMLNode:childElement] objectForKey:[TBXML elementName:childElement]]];
[elementDict setObject:[NSArray arrayWithArray:items] forKey:[TBXML elementName:childElement]];
[items release]; items = nil;
}
childElement = childElement->nextSibling;
}
} else if ([TBXML textForElement:element] != nil && [TBXML textForElement:element].length>0) {
if ([elementDict count]>0) {
[elementDict setObject:[TBXML textForElement:element] forKey:@"text"];
} else {
[elementDict setObject:[TBXML textForElement:element] forKey:[TBXML elementName:element]];
}
}
NSDictionary *resultDict = nil;
if ([elementDict count]>0) {
if ([elementDict valueForKey:[TBXML elementName:element]] == nil) {
resultDict = [NSDictionary dictionaryWithObject:elementDict forKey:[TBXML elementName:element]];
} else {
resultDict = [NSDictionary dictionaryWithDictionary:elementDict];
}
}
[elementDict release]; elementDict = nil;
return resultDict;
}
+ (NSDictionary*)dictionaryWithXMLData:(NSData*)data
{
TBXML *tbxml = [TBXML tbxmlWithXMLData:data];
if (!tbxml.rootXMLElement) {
return nil;
}
return [self dictionaryWithXMLNode:tbxml.rootXMLElement];
}
@end
#import "TBXML.h"
@interface TBXML (TBXML_NSDictionary)
+ (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element;
+ (NSDictionary*)dictionaryWithXMLData:(NSData*)data;
@end
#import "TBXML+NSDictionary.h"
@implementation TBXML (TBXML_NSDictionary)
+ (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element
{
NSMutableDictionary *elementDict = [[NSMutableDictionary alloc] init];
TBXMLAttribute *attribute = element->firstAttribute;
while (attribute) {
[elementDict setObject:[TBXML attributeValue:attribute] forKey:[TBXML attributeName:attribute]];
attribute = attribute->next;
}
TBXMLElement *childElement = element->firstChild;
if (childElement) {
while (childElement) {
if ([elementDict objectForKey:[TBXML elementName:childElement]] == nil) {
[elementDict addEntriesFromDictionary:[self dictionaryWithXMLNode:childElement]];
} else if ([[elementDict objectForKey:[TBXML elementName:childElement]] isKindOfClass:[NSArray class]]) {
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:[elementDict objectForKey:[TBXML elementName:childElement]]];
[items addObject:[[self dictionaryWithXMLNode:childElement] objectForKey:[TBXML elementName:childElement]]];
[elementDict setObject:[NSArray arrayWithArray:items] forKey:[TBXML elementName:childElement]];
[items release]; items = nil;
} else {
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[elementDict objectForKey:[TBXML elementName:childElement]]];
[items addObject:[[self dictionaryWithXMLNode:childElement] objectForKey:[TBXML elementName:childElement]]];
[elementDict setObject:[NSArray arrayWithArray:items] forKey:[TBXML elementName:childElement]];
[items release]; items = nil;
}
childElement = childElement->nextSibling;
}
} else if ([TBXML textForElement:element] != nil && [TBXML textForElement:element].length>0) {
if ([elementDict count]>0) {
[elementDict setObject:[TBXML textForElement:element] forKey:@"text"];
} else {
[elementDict setObject:[TBXML textForElement:element] forKey:[TBXML elementName:element]];
}
}
NSDictionary *resultDict = nil;
if ([elementDict count]>0) {
if ([elementDict valueForKey:[TBXML elementName:element]] == nil) {
resultDict = [NSDictionary dictionaryWithObject:elementDict forKey:[TBXML elementName:element]];
} else {
resultDict = [NSDictionary dictionaryWithDictionary:elementDict];
}
}
[elementDict release]; elementDict = nil;
return resultDict;
}
+ (NSDictionary*)dictionaryWithXMLData:(NSData*)data
{
TBXML *tbxml = [TBXML tbxmlWithXMLData:data];
if (!tbxml.rootXMLElement) {
return nil;
}
return [self dictionaryWithXMLNode:tbxml.rootXMLElement];
}
@end
@crsantos
Copy link

Mate, thanks for this awesome category! <3

@fuxlud
Copy link

fuxlud commented Mar 7, 2013

Wow this is just great!!!!!

@NunciosChums
Copy link

thank you very much!!!

@ketansmodha
Copy link

Great..!!! Thanks for such a good category.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment