前端之家收集整理的这篇文章主要介绍了
NSXMLParser XML 解析 解压,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#import "Http.h"
@interface Http ()
@end
@implementation Http
@synthesize requestAddr;
@synthesize loginJudge;
-(id)initWithRequestStyle:(HTTPREQUESTTYPE)type paramArray:(NSDictionary*)params
{
paramDict = params;
finished = NO;
//[self switchHttpUrlRequest:type];
self.requestAddr = [self switchHttpUrlRequest:type];
return self;
}
//url拼接与判断
-(NSString *)switchHttpUrlRequest:(HTTPREQUESTTYPE)type
{
NSMutableString *addr = [[[NSMutableString alloc]initWithString:@"http:XXXXXXXXXXXX"]autorelease];
ViewController *viewcontroller = [[ViewController alloc]init];
[viewcontroller autorelease];
switch (type)
{
case EPAYLOGIN:
[addr appendFormat:@"getlogin?Account=%@&Password=%@%",[paramDict objectForKey:@"useName"],[paramDict objectForKey:@"usePwd"]];
break;
default:
break;
}
NSLog(@"addrHpptRequest:%@",addr);
return addr;
}
-(void)requestAndconnect
{
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init] autorelease];
requestAddr = [requestAddr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"requestAddr%@",requestAddr);
[request setURL:[NSURL URLWithString:requestAddr]];
[request setHTTPMethod:@"GET"];
[request setTimeoutInterval:10];
NSURLConnection *conn = [[[NSURLConnection alloc]initWithRequest:request delegate:self] autorelease];
if(conn)
{
receivedData = [[NSMutableData data] retain];
while(!finished)
{
timeout ++;
NSLog(@"outtime:%d",timeout);
if(timeout>5)
{
finished = YES;
}
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
}
else
{
NSLog(@"失败");
}
}
/*
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
finished = YES;
//etrafficAppDelegate *app =[[UIApplication sharedApplication] delegate];
//app.moudle.netWorkComplite = NO;
NSLog(@"connection error");
}
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSLog(@"receivedDate:%@",receivedData);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
EpayLogin *epayloginxml;
switch (curType)
{
case EPAYLOGIN:
NSLog(@"sssssss");
epayloginxml = [[EpayLogin alloc]initWithData:receivedData];
NSMutableArray *aa = [[NSMutableArray alloc]init];
aa = [epayloginxml beginParser];
NSDictionary *a = [[NSDictionary alloc]init];
a = [aa objectAtIndex:0];
NSLog(@"aaaaaaaaaa:%@",[aa objectAtIndex:0]);
NSLog(@"aa%@",[a objectForKey:@"Transreturn"]);
LoginJudge *test = [[LoginJudge alloc]init];
test.loginarray = aa;
self.loginJudge =[test autorelease];
NSLog(@"retainCount:%d",[self.loginJudge retainCount]);
NSLog(@"loginarray:%@",[self.loginJudge.loginarray objectAtIndex:0]);
break;
default:
break;
}
}
@end
@class LoginJudge;
#import <UIKit/UIKit.h>
#import <zlib.h>
#import "ParserForMainReturn.h"
#import "ViewController.h"
#import "EpayLogin.h"
#import "LoginJudge.h"
enum _HttpRequestType
{
EPAYLOGIN,};
typedef NSUInteger HTTPREQUESTTYPE;
@interface Http : NSObject
{
NSMutableData *receivedData;
NSDictionary *paramDict;
BOOL finished;
HTTPREQUESTTYPE curType;
int timeout;
}
@property(nonatomic,retain)NSString *requestAddr;
@property(nonatomic,retain)LoginJudge *loginJudge;
-(id)initWithRequestStyle:(HTTPREQUESTTYPE)type paramArray:(NSDictionary*)params;
-(NSString *)switchHttpUrlRequest:(HTTPREQUESTTYPE)type;
-(void)requestAndconnect;
@end
#import "ParserForMainReturn.h"
#import "Utilities.h"
#import "NSDataGZipAdditions.h"
#import <zlib.h>
@implementation ParserForMainReturn
@synthesize data;
-(id)initWithData:(NSData*)pdata
{
if (!(self=[super init])) return nil;
if (self) {
self.data = pdata;
mStr = [[NSMutableString alloc]init ];
}
return self;
}
-(NSData*)beginParser
{
parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
// The parser calls methods in this class
[parser parse];
[parser release];
return mData;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"return"]) {
mainElement = MRETURN;
return;
}
mainElement = 99;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
switch (mainElement) {
case MRETURN:
[mStr appendFormat:@"%@",string ];
NSLog(@"mStr:%@",mStr);
break;
default:
break;
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"return"]) {
return;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSData *unBase64 = [Utilities base64toByte:mStr];
//NSData *unBase64 = [Utilities base64toByte: retStr];
NSLog(@"len %d",[unBase64 length]);
if ([unBase64 length] == 0) {
return;
}
//mData = [NSDataGZipAdditions dataWithCompressedBytes:[unBase64 bytes] length:[unBase64 length]];
mData = [self uncompressZippedData:unBase64];
NSLog(@"mData %@",[NSString stringWithUTF8String:[mData bytes]]);
}
-(NSData *)uncompressZippedData:(NSData *)compressedData
{
if ([compressedData length] == 0) return compressedData;
unsigned full_length = [compressedData length];
unsigned half_length = [compressedData length] / 2;
NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
BOOL done = NO;
int status;
z_stream strm;
strm.next_in = (Bytef *)[compressedData bytes];
strm.avail_in = [compressedData length];
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if (inflateInit2(&strm,(15+32)) != Z_OK) return nil;
while (!done) {
// Make sure we have enough room and reset the lengths.
if (strm.total_out >= [decompressed length]) {
[decompressed increaseLengthBy: half_length];
}
strm.next_out = [decompressed mutableBytes] + strm.total_out;
strm.avail_out = [decompressed length] - strm.total_out;
// Inflate another chunk.
status = inflate (&strm,Z_SYNC_FLUSH);
if (status == Z_STREAM_END) {
done = YES;
} else if (status != Z_OK) {
break;
}
}
if (inflateEnd (&strm) != Z_OK) return nil;
// Set real length.
if (done) {
[decompressed setLength: strm.total_out];
NSLog(@"Y len:%lu",strm.total_out);
return decompressed;
//return [NSData dataWithData: decompressed];
} else {
NSLog(@"no");
return nil;
}
}
-(void)dealloc
{
self.data = nil;
// [countyList release];
[mStr release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
enum __mainElement {
MRETURN
};
typedef NSUInteger _mainElement;
@interface ParserForMainReturn : NSObject <NSXMLParserDelegate>{
NSData *data;
NSXMLParser *parser;
_mainElement mainElement;
NSData *mData;
NSMutableString *mStr;
// NSMutableArray *mainList;
// NSMutableDictionary *mainInfo;
}
@property(nonatomic,retain)NSData *data;
-(id)initWithData:(NSData*)pdata ;
-(NSData*)beginParser;
-(NSData *)uncompressZippedData:(NSData *)compressedData ;
@end
原文链接:https://www.f2er.com/xml/300362.html