Advertisement
Guest User

XMLReader.m

a guest
May 31st, 2013
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  XMLReader.m
  3. //
  4. //  Created by Troy on 9/18/10.
  5. //  Copyright 2010 Troy Brant. All rights reserved.
  6. //
  7.  
  8. #import "XMLReader.h"
  9.  
  10. NSString *const kXMLReaderTextNodeKey = @"text";
  11.  
  12. @interface XMLReader (Internal)
  13.  
  14. - (id)initWithError:(NSError **)error;
  15. - (NSDictionary *)objectWithData:(NSData *)data;
  16.  
  17. @end
  18.  
  19. @implementation XMLReader
  20.  
  21. #pragma mark -
  22. #pragma mark Public methods
  23.  
  24. + (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error
  25. {
  26.     XMLReader *reader = [[XMLReader alloc] initWithError:error];
  27.     NSDictionary *rootDictionary = [reader objectWithData:data];
  28.     [reader release];
  29.     return rootDictionary;
  30. }
  31.  
  32. + (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error
  33. {
  34.     NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  35.     return [XMLReader dictionaryForXMLData:data error:error];
  36. }
  37.  
  38. #pragma mark -
  39. #pragma mark Parsing
  40.  
  41. - (id)initWithError:(NSError*)error
  42. {
  43.     if (self = [super init])
  44.     {
  45.         errorPointer = error;
  46.     }
  47.     return self;
  48. }
  49.  
  50. - (void)dealloc
  51. {
  52.     [dictionaryStack release];
  53.     [textInProgress release];
  54.     [super dealloc];
  55. }
  56.  
  57. - (NSDictionary *)objectWithData:(NSData *)data
  58. {
  59.     // Clear out any old data
  60.     [dictionaryStack release];
  61.     [textInProgress release];
  62.    
  63.     dictionaryStack = [[NSMutableArray alloc] init];
  64.     textInProgress = [[NSMutableString alloc] init];
  65.    
  66.     // Initialize the stack with a fresh dictionary
  67.     [dictionaryStack addObject:[NSMutableDictionary dictionary]];
  68.    
  69.     // Parse the XML
  70.     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  71.     parser.delegate =(id)self;
  72.     BOOL success = [parser parse];
  73.    
  74.     // Return the stack's root dictionary on success
  75.     if (success)
  76.     {
  77.         NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
  78.         return resultDict;
  79.     }
  80.     return nil;
  81. }
  82.  
  83. #pragma mark -
  84. #pragma mark NSXMLParserDelegate methods
  85.  
  86. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  87. {
  88.    
  89.     //NSLog(@"============ element Name %@",elementName);
  90.     //NSLog(@"============ attribute %@",attributeDict);
  91.    
  92.     // Get the dictionary for the current level in the stack
  93.     NSMutableDictionary *parentDict = [dictionaryStack lastObject];
  94.  
  95.     // Create the child dictionary for the new element, and initilaize it with the attributes
  96.     NSMutableDictionary *childDict = [NSMutableDictionary dictionary];
  97.     [childDict addEntriesFromDictionary:attributeDict];
  98.    
  99.     //NSLog(@"parent dic======== %@",parentDict);
  100.     //NSLog(@"child dic ======== %@",childDict);
  101.    
  102.     // If there's already an item for this key, it means we need to create an array
  103.     id existingValue = [parentDict objectForKey:elementName];
  104.     if (existingValue)
  105.     {
  106.         NSMutableArray *array = nil;
  107.         if ([existingValue isKindOfClass:[NSMutableArray class]])
  108.         {
  109.             // The array exists, so use it
  110.             array = (NSMutableArray *) existingValue;
  111.         }
  112.         else{
  113.             // Create an array if it doesn't exist
  114.             array = [NSMutableArray array];
  115.             [array addObject:existingValue];
  116.             // Replace the child dictionary with an array of children dictionaries
  117.             [parentDict setObject:array forKey:elementName];
  118.         }
  119.        
  120.         // Add the new child dictionary to the array
  121.         [array addObject:childDict];
  122.     }else{
  123.         // No existing value, so update the dictionary
  124.         [parentDict setObject:childDict forKey:elementName];
  125.     }
  126.    
  127.     // Update the stack
  128.     [dictionaryStack addObject:childDict];
  129. }
  130.  
  131. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  132. {
  133.    
  134.     ////NSLog(@"end ========= element name %@",elementName);
  135.    
  136.     // Update the parent dict with text info
  137.     NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];
  138.    
  139.     // Set the text property
  140.     if ([textInProgress length] > 0)
  141.     {
  142.         [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
  143.  
  144.         // Reset the text
  145.         [textInProgress release];
  146.         textInProgress = [[NSMutableString alloc] init];
  147.     }
  148.    
  149.     // Pop the current dict
  150.     [dictionaryStack removeLastObject];
  151. }
  152.  
  153. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  154. {
  155.     // Build the text value
  156.     [textInProgress appendString:string];
  157. }
  158.  
  159. - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
  160. {
  161.     // Set the error pointer to the parser's error object
  162.     errorPointer = parseError;
  163. }
  164.  
  165. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement