1:JSON转XML---通常把JSON数据写到Plist文件即可;
2:XML数据转JSON--
使用开源类 XMLReader 先XML数据先转换为 NSDictionary 即可
3: 开源类XMLReader
1 // 2 // XMLReader.h 3 // 4 // 5 /* 6 本类使用方法: 7 1:在使用的页面里面 导入本类 #import "XMLReader.h" 8 2: 调用XMLReader 的两个类方法 就可以了,就可以返回字典数据; 9 3:得到 NSDictionary数据,就是JSON 数据了;10 11 */12 #import13 14 15 @interface XMLReader : NSObject 16 {17 NSMutableArray *dictionaryStack;18 NSMutableString *textInProgress;19 NSError **errorPointer;20 }21 22 //XML Data数据转换为 字典的方法;23 + (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer;24 + (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer;25 26 @end
1 // 2 // XMLReader.m 3 // 4 5 #import "XMLReader.h" 6 7 NSString *const kXMLReaderTextNodeKey = @"text"; 8 9 @interface XMLReader (Internal) 10 11 - (id)initWithError:(NSError **)error; 12 - (NSDictionary *)objectWithData:(NSData *)data; 13 14 @end 15 16 17 @implementation XMLReader 18 19 #pragma mark - 20 #pragma mark -公有的方法 21 22 //调用以下两个类方法即可; 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 以下为 私有处理方法,可以不关注; 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 = 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 81 return nil; 82 } 83 84 #pragma mark - 85 #pragma mark NSXMLParserDelegate methods 86 87 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 88 { 89 // Get the dictionary for the current level in the stack 90 NSMutableDictionary *parentDict = [dictionaryStack lastObject]; 91 92 // Create the child dictionary for the new element, and initilaize it with the attributes 93 NSMutableDictionary *childDict = [NSMutableDictionary dictionary]; 94 [childDict addEntriesFromDictionary:attributeDict]; 95 96 // If there's already an item for this key, it means we need to create an array 97 id existingValue = [parentDict objectForKey:elementName]; 98 if (existingValue) 99 {100 NSMutableArray *array = nil;101 if ([existingValue isKindOfClass:[NSMutableArray class]])102 {103 // The array exists, so use it104 array = (NSMutableArray *) existingValue;105 }106 else107 {108 // Create an array if it doesn't exist109 array = [NSMutableArray array];110 [array addObject:existingValue];111 112 // Replace the child dictionary with an array of children dictionaries113 [parentDict setObject:array forKey:elementName];114 }115 116 // Add the new child dictionary to the array117 [array addObject:childDict];118 }119 else120 {121 // No existing value, so update the dictionary122 [parentDict setObject:childDict forKey:elementName];123 }124 125 // Update the stack126 [dictionaryStack addObject:childDict];127 }128 129 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName130 {131 // Update the parent dict with text info132 NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];133 134 // Set the text property135 if ([textInProgress length] > 0)136 {137 [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];138 139 // Reset the text140 [textInProgress release];141 textInProgress = [[NSMutableString alloc] init];142 }143 144 // Pop the current dict145 [dictionaryStack removeLastObject];146 }147 148 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string149 {150 // Build the text value151 [textInProgress appendString:string];152 }153 154 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError155 {156 // Set the error pointer to the parser's error object157 *errorPointer = parseError;158 }159 160 @end
4:GitHub下载: