|
|
<?xml version="1.0" encoding="utf-8"?> <CTItemListRSS |
|
version="1.0"> | |
<itemsInfo> | |
<status_code>0</status_code> | |
<itemsTotal>8</itemsTotal> | |
<msg /> | |
</itemsInfo> | |
<items> | |
<item> | |
<CATEGORY_ID>001</CATEGORY_ID> | |
<CATEGORY_NAME><![CDATA[CNTV]]></CATEGORY_NAME> | |
<logo_URL><![CDATA[..........]]></logo_URL> | |
</item> | |
<item> | |
<CATEGORY_ID>002</CATEGORY_ID> | |
<CATEGORY_NAME><![CDATA[CCTV]]></CATEGORY_NAME> | |
<logo_URL></logo_URL> | |
</item> | |
......... | |
</items> | |
</CTItemListRSS> |
1、首先寫model
#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
#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
#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
#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];
@H_404_963@ [[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];
@H_404_963@ [[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] ;
@H_404_963@ 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];
@H_404_963@ [[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];
@H_404_963@ [[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] ;
@H_404_963@ 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];
@H_404_963@ [[NSNotificationCenter defaultCenter] postNotificationName:@"networkPopupView" object:error];}];
[op start];
}
@end