Skip to content

Instantly share code, notes, and snippets.

@romainbriche
Created February 27, 2012 09:07

Revisions

  1. Romain Briche revised this gist Feb 27, 2012. No changes.
  2. Romain Briche revised this gist Feb 27, 2012. No changes.
  3. Romain Briche revised this gist Feb 27, 2012. 2 changed files with 83 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions TBXMLNSDictionary.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #import "TBXML.h"

    @interface TBXML (TBXML_NSDictionary)
    + (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element;
    + (NSDictionary*)dictionaryWithXMLData:(NSData*)data;
    @end
    77 changes: 77 additions & 0 deletions TBXMLNSDictionary.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #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
  4. Romain Briche revised this gist Feb 27, 2012. 2 changed files with 7 additions and 6 deletions.
    6 changes: 6 additions & 0 deletions TBXML+NSDictionary.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #import "TBXML.h"

    @interface TBXML (TBXML_NSDictionary)
    + (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element;
    + (NSDictionary*)dictionaryWithXMLData:(NSData*)data;
    @end
    7 changes: 1 addition & 6 deletions gistfile1.m → TBXML+NSDictionary.m
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,4 @@
    #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
  5. Romain Briche created this gist Feb 27, 2012.
    82 changes: 82 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    #import "TBXML.h"

    @interface TBXML (TBXML_NSDictionary)
    + (NSDictionary*)dictionaryWithXMLNode:(TBXMLElement*)element;
    + (NSDictionary*)dictionaryWithXMLData:(NSData*)data;
    @end

    @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