Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.90 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. nsxml parser issue
  2. username =         {
  3.         text = akhildas;
  4.         type = string;
  5.     };
  6.        
  7. username = akhildas
  8.        
  9. - (NSDictionary *)objectWithData:(NSData *)data
  10. {
  11. // Clear out any old data
  12. [dictionaryStack release];
  13. [textInProgress release];
  14.  
  15. dictionaryStack = [[NSMutableArray alloc] init];
  16. textInProgress = [[NSMutableString alloc] init];
  17.  
  18. // Initialize the stack with a fresh dictionary
  19. [dictionaryStack addObject:[NSMutableDictionary dictionary]];
  20.  
  21. // Parse the XML
  22. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  23. parser.delegate = self;
  24. BOOL success = [parser parse];
  25.  
  26. // Return the stack's root dictionary on success
  27. if (success)
  28. {
  29.     NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
  30.     return resultDict;
  31. }
  32.  
  33. return nil;
  34. }
  35.  
  36. #pragma mark -
  37. #pragma mark NSXMLParserDelegate methods
  38.  
  39. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  40. {
  41.  
  42.  
  43. // Get the dictionary for the current level in the stack
  44. NSMutableDictionary *parentDict = [dictionaryStack lastObject];
  45.  
  46. // Create the child dictionary for the new element, and initilaize it with the attributes
  47. NSMutableDictionary *childDict = [NSMutableDictionary dictionary];
  48. [childDict addEntriesFromDictionary:attributeDict];
  49.  
  50. // If there's already an item for this key, it means we need to create an array
  51. id existingValue = [parentDict objectForKey:elementName];
  52. if (existingValue)
  53. {
  54.     NSMutableArray *array = nil;
  55.     if ([existingValue isKindOfClass:[NSMutableArray class]])
  56.     {
  57.         // The array exists, so use it
  58.         array = (NSMutableArray *) existingValue;
  59.     }
  60.     else
  61.     {
  62.         // Create an array if it doesn't exist
  63.         array = [NSMutableArray array];
  64.         [array addObject:existingValue];
  65.  
  66.         // Replace the child dictionary with an array of children dictionaries
  67.         [parentDict setObject:array forKey:elementName];
  68.     }
  69.  
  70.     // Add the new child dictionary to the array
  71.     [array addObject:childDict];
  72.  
  73. }
  74. else
  75. {
  76.     // No existing value, so update the dictionary
  77.     [parentDict setObject:childDict forKey:elementName];
  78. }
  79.  
  80. // Update the stack
  81. [dictionaryStack addObject:childDict];
  82. }
  83.  
  84. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  85. {
  86.  
  87.  
  88. // Update the parent dict with text info
  89. NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];
  90.  
  91. // Set the text property
  92. if ([textInProgress length] > 0)
  93. {
  94.     [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
  95.  
  96.     // Reset the text
  97.     [textInProgress release];
  98.     textInProgress = [[NSMutableString alloc] init];
  99. }
  100.  
  101. // Pop the current dict
  102. [dictionaryStack removeLastObject];
  103. }
  104. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  105. {
  106. // Build the text value
  107. [textInProgress appendString:string];
  108. }