使用XmlReader类读取XML数据之
下面形式的一個XML怎麼解析呢?
1、首先寫model
我們可以新建兩個model :HotJobCategoryItems,HotJobCategory
HotJobCategoryItems.h
#import <Foundation/Foundation.h> #import "ItemsInfo.h" @interface HotJobCategoryItems : NSObject @property ItemsInfo *itemInfo; @property NSMutableArray *hotJobCategoryList; -(void) initWithDictionary:(NSDictionary *)dictionary; -(NSString *) toString; @end
#import "HotJobCategoryItems.h" #import "HotJobCategory.h" @implementation HotJobCategoryItems @synthesize itemInfo; @synthesize hotJobCategoryList; -(void) initWithDictionary:(NSDictionary *)dictionary{ self.hotJobCategoryList=[NSMutableArray new]; NSArray *itemListArray = [dictionary objectForKey:@"item"]; if (![itemListArray isKindOfClass:[NSArray class]]) { itemListArray = [NSArray arrayWithObject:itemListArray]; } for(int i = 0; i < itemListArray.count; i++) { HotJobCategory *hotJobCategory = [[HotJobCategory alloc] init]; [hotJobCategory initWithDictionary:[itemListArray objectAtIndex:i]]; [self.hotJobCategoryList addObject:hotJobCategory]; hotJobCategory = nil; } itemListArray = nil; } -(NSString *) toString{ NSString *returnString = [NSString stringWithFormat:@"ItemInfo:n statusCode:%@ n itemsTotal:%@ n msg:%@ n",itemInfo.statusCode,itemInfo.itemsTotal,itemInfo.msg]; for (HotJobCategory *hotJobCategory in self.hotJobCategoryList) { returnString = [returnString stringByAppendingString:hotJobCategory.toString]; } return returnString;
} @end
HotJobCategory.h
#import <Foundation/Foundation.h> @interface HotJobCategory : NSObject @property NSNumber *jobCategoryId; @property NSString *jobCategoryName; @property NSString *logoUrl; -(void) initWithDictionary:(NSDictionary *)dictionary; -(NSString *) toString; @end HotJobCategory.m #import "HotJobCategory.h" @implementation HotJobCategory @synthesize jobCategoryId; @synthesize jobCategoryName; @synthesize logoUrl; -(void) initWithDictionary:(NSDictionary *)dictionary { self.jobCategoryId = [[dictionary objectForKey:@"JOBCATEGORY_ID"]objectForKey:DEFAULT_XML_TAG]; self.jobCategoryName = [[dictionary objectForKey:@"JOBCATEGORY_NAME"]objectForKey:DEFAULT_XML_TAG]; self.logoUrl = [[dictionary objectForKey:@"LOGO_URL"]objectForKey:DEFAULT_XML_TAG]; } -(NSString *) toString { return [NSString stringWithFormat:@"HotJobCategory:n jobCategoryId:%@ n jobCategoryName:%@ n logoUrl:%@ n",self.jobCategoryId,self.jobCategoryName,self.logoUrl]; } @end 2、在項目中加入XMLReader,網上搜!3、新建XMLManager
XMLManager.h
#import <Foundation/Foundation.h> #import "ItemsInfo.h" @protocol XmlManagerDelegate @optional -(void)handleParse:(BOOL)success xmlData:(NSDictionary *)xmlData itemInfo:(ItemsInfo *)itemInfo xmlType:(int)xmlType; -(void)handleParse:(BOOL)success data:(NSData*)data xmlType:(int)xmlType; -(void)handleLoginSeesionFail:(ItemsInfo *)itemInfo xmlType:(int)xmlType; @end @interface XMLManager : NSObject<NSXMLParserDelegate> -(void) parseXML:(NSString *) urlString xmlType:(int)xmlType;//get方法解析 -(void) parsePostXML:(NSString *) urlString inputParams:(NSString *)inputParams xmlType:(int)xmlType;/ /post方法解析 -(void) parsePost:(NSString *) urlString inputParams:(NSString *)inputParams xmlType:(int)xmlType; @property (nonatomic,assign) id<XmlManagerDelegate> delegate; @end
XMLManager.m
#import "XMLManager.h" #import "XMLReader.h" #import "ItemsInfo.h"
@implementation XMLManager @synthesize delegate;
-(void) parseXML:(NSString *) urlString xmlType:(int)xmlType{ // if (showLog) { // NSLog(@"ParseXML URL : %@",urlString); // } NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// [AFXMLRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFHTTPRequestOperation *op =[[AFHTTPRequestOperation alloc]initWithRequest:request]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,NSData *data){ NSError *error = nil; NSDictionary *dict = [XMLReader dictionaryForXMLData:data options:XMLReaderOptionsProcessNamespaces error:&error]; NSDictionary *cTItemListRSS = [dict objectForKey:@"CTItemListRSS"];
if([cTItemListRSS count] == 0) { if (showLog) { NSString *returnData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ; NSLog(@"ParseXML URL %@ n Exception: %@",urlString,returnData);
} [delegate handleParse:NO xmlData:nil itemInfo:nil xmlType:xmlType]; [[NSNotificationCenter defaultCenter] postNotificationName:@"networkPopupView" object:error]; } else {
ItemsInfo *itemInfo = [[ItemsInfo alloc] init]; [itemInfo initWithDictionary:[cTItemListRSS objectForKey:XML_TAG_ITEMSINFO]];
if (showLog) { NSLog(@"ParseXML URL %@ n ItemInfo : %@",itemInfo.toString); }
switch ([itemInfo.statusCode integerValue]) { case 3: { [delegate handleParse:NO xmlData:cTItemListRSS itemInfo:itemInfo xmlType:xmlType ]; // [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSeesionPopupView" object:itemInfo.msg]; [delegate handleLoginSeesionFail:itemInfo xmlType:xmlType]; break; } default: { [delegate handleParse:YES xmlData:cTItemListRSS itemInfo:itemInfo xmlType:xmlType ]; break; } }
itemInfo = nil; } dict = nil; cTItemListRSS = nil;
}failure:^(AFHTTPRequestOperation *operation,NSError *error){ if (showLog) { NSLog(@"ParseXML URL %@ n Error : %@",error); } [delegate handleParse:NO xmlData:nil itemInfo:nil xmlType:xmlType]; [[NSNotificationCenter defaultCenter] postNotificationName:@"networkPopupView" object:error]; }];
[op start];
}
-(void) parsePostXML:(NSString *) urlString inputParams:(NSString *)inputParams xmlType:(int)xmlType{ if (showLog) { NSLog(@"ParsePostXML URL : %@",urlString); NSLog(@"ParsePostXML inputParams : %@",inputParams); }
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; NSData *postData = [inputParams dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData];
// [AFXMLRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFHTTPRequestOperation *op =[[AFHTTPRequestOperation alloc]initWithRequest:request]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,NSData *data){ NSError *error = nil;
NSDictionary *dict = [XMLReader dictionaryForXMLData:data options:XMLReaderOptionsProcessNamespaces error:&error]; NSDictionary *cTItemListRSS = [dict objectForKey:@"CTItemListRSS"];
if([cTItemListRSS count] == 0) { if (showLog) { NSString *returnData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ; NSLog(@"ParsePostXML URL Exception: %@",returnData);
} [delegate handleParse:NO xmlData:nil itemInfo:nil xmlType:xmlType]; [[NSNotificationCenter defaultCenter] postNotificationName:@"networkPopupView" object:error]; } else {
ItemsInfo *itemInfo = [[ItemsInfo alloc] init]; [itemInfo initWithDictionary:[cTItemListRSS objectForKey:XML_TAG_ITEMSINFO]];
if (showLog) { NSLog(@"ParsePostXML URL ItemInfo : %@",itemInfo.toString); }
switch ([itemInfo.statusCode integerValue]) { case 3: { // [delegate handleParse:NO xmlData:cTItemListRSS itemInfo:itemInfo xmlType:xmlType ]; // [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSeesionPopupView" object:itemInfo.msg]; [delegate handleLoginSeesionFail:itemInfo xmlType:xmlType]; break; } default: { [delegate handleParse:YES xmlData:cTItemListRSS itemInfo:itemInfo xmlType:xmlType ]; break; } } itemInfo = nil; } dict = nil; cTItemListRSS = nil;
}failure:^(AFHTTPRequestOperation *operation,NSError *error){ if (showLog) { NSLog(@"ParsePostXML URL Error : %@",error); } [delegate handleParse:NO xmlData:nil itemInfo:nil xmlType:xmlType]; [[NSNotificationCenter defaultCenter] postNotificationName:@"networkPopupView" object:error]; }];
[op start]; } //post -(void) parsePost:(NSString *) urlString inputParams:(NSString *)inputParams xmlType:(int)xmlType{ if (showLog) { NSLog(@"parsePost URL : %@",urlString); NSLog(@"parsePost inputParams : %@",inputParams); } NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; NSData *postData = [inputParams dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData];
// [AFXMLRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFHTTPRequestOperation *op =[[AFHTTPRequestOperation alloc]initWithRequest:request]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,NSData *data){
if(data == nil) { if (showLog) { NSString *returnData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ; NSLog(@"parsePost URL Exception: %@",returnData); } [delegate handleParse:NO data:nil xmlType:xmlType]; } else { [delegate handleParse:YES data:data xmlType:xmlType]; } }failure:^(AFHTTPRequestOperation *operation,error); } [delegate handleParse:NO xmlData:nil itemInfo:nil xmlType:xmlType]; [[NSNotificationCenter defaultCenter] postNotificationName:@"networkPopupView" object:error]; }];
[op start]; } @end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |