-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXML2NSDictionary.m
160 lines (132 loc) · 5.14 KB
/
XML2NSDictionary.m
1
//// XML2NSDictionary.m//// Copyright (c) 2010 Arol Viñolas// // Permission is hereby granted, free of charge, to any// person obtaining a copy of this software and associated// documentation files (the "Software"), to deal in the// Software without restriction, including without limitation// the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the// Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // // The above copyright notice and this permission notice// shall be included in all copies or substantial portions of// the Software.// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.//#import "XML2NSDictionary.h"@implementation XML2NSDictionary@synthesize result;- (id)init { // Assign self to value returned by super's designated initializer // Designated initializer for NSObject is init self = [super init]; if (self) { stackNames = [[NSMutableArray alloc] init]; currentName = [[NSString alloc] initWithString:@""]; result = nil; } return self;}-(void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"XML2NSDictionary: Starts document");}-(void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"XML2NSDictionary: Ends Document WITH ELEMENT %@", currentName); result = [[NSMutableDictionary alloc] init]; [result setValue:currentDictionary forKey:currentName];}-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ NSLog(@"XML2NSDictionary: Starts ELEMENT %@", elementName); //Checking if last name is equal than this one if ([currentName isEqualToString:elementName]) { NSLog(@"Canviem el Dictionary per l'array"); isArray = YES; //Pulling dictionary from stack and pushing an array if ([[stackNames lastObject] isKindOfClass:[NSMutableDictionary class]]) { currentDictionary = [stackNames lastObject]; currentArray = [NSMutableArray arrayWithArray:[currentDictionary allValues]]; [stackNames removeLastObject]; [stackNames addObject:currentArray]; //[currentArray release]; //[currentDictionary release]; } } currentName = elementName; currentDictionary = [[NSMutableDictionary alloc] init]; [stackNames addObject:currentName]; [stackNames addObject:currentDictionary];}-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ NSLog(@"XML2NSDictionary: Ends ELEMENT %@", elementName); currentName = elementName; //Getting and pulling node data //NSObject *data = [stackNames lastObject]; NSObject *data = [[stackNames lastObject] mutableCopy]; /* VERSIO TEDIOSA DE LA COPIA if ([[stackNames lastObject] isKindOfClass:[NSMutableArray class]]) { //We copy the actual array }else if ([[stackNames lastObject] isKindOfClass:[NSMutableDictionary class]]) { //We copy the actual dictionary }*/ [stackNames removeLastObject]; //Pulling node name [stackNames removeLastObject]; //Getting parent node data NSObject *temp = [stackNames lastObject]; if (temp) { if ([temp isKindOfClass:[NSMutableArray class]]) { //We work with an array [(NSMutableArray*)temp addObject:data]; NSLog(@"Pushing %@ called %@ in ARRAY", [data class], currentName); }else { //We work with a dictionary [(NSMutableDictionary*)temp setValue:data forKey:currentName]; NSLog(@"Pushing %@ called %@ in DICTIONARY of", [data class], currentName); } }else { currentDictionary = (NSMutableDictionary*)data; NSLog(@"NAMES STACK IS EMPTY!!!"); } }-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{ NSLog(@"XML2NSDictionary: Found CDATA %@", [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]); //Pulling NSDictionary [stackNames removeLastObject]; //Pushing node data NSString *nodeData = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]; [stackNames addObject:nodeData];}-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if ([[[string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]) return; NSLog(@"XML2NSDictionary: Found CHARS %@", string); //Pulling NSDictionary [stackNames removeLastObject]; //Pushing node data NSString *nodeData = string; [stackNames addObject:nodeData];}-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{ NSLog(@"Error mentre es parsejava el document");}@end