微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用XmlReader类读取XML数据之

下面形式的一個XML怎麼解析呢?


<?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

我們可以新建兩個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


HotJobCategoryItems.m

#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

原文地址:https://www.jb51.cc/xml/299054.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。