NSXMLParser XML 解析 解压

前端之家收集整理的这篇文章主要介绍了NSXMLParser XML 解析 解压前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #import "Http.h"
  2.  
  3.  
  4.  
  5. @interface Http ()
  6. @end
  7.  
  8. @implementation Http
  9. @synthesize requestAddr;
  10. @synthesize loginJudge;
  11. -(id)initWithRequestStyle:(HTTPREQUESTTYPE)type paramArray:(NSDictionary*)params
  12. {
  13. paramDict = params;
  14. finished = NO;
  15. //[self switchHttpUrlRequest:type];
  16. self.requestAddr = [self switchHttpUrlRequest:type];
  17. return self;
  18. }
  19. //url拼接与判断
  20. -(NSString *)switchHttpUrlRequest:(HTTPREQUESTTYPE)type
  21. {
  22. NSMutableString *addr = [[[NSMutableString alloc]initWithString:@"http:XXXXXXXXXXXX"]autorelease];
  23. ViewController *viewcontroller = [[ViewController alloc]init];
  24. [viewcontroller autorelease];
  25. switch (type)
  26. {
  27. case EPAYLOGIN:
  28. [addr appendFormat:@"getlogin?Account=%@&Password=%@%",[paramDict objectForKey:@"useName"],[paramDict objectForKey:@"usePwd"]];
  29. break;
  30. default:
  31. break;
  32. }
  33. NSLog(@"addrHpptRequest:%@",addr);
  34. return addr;
  35. }
  36. -(void)requestAndconnect
  37. {
  38. NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init] autorelease];
  39. requestAddr = [requestAddr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  40. NSLog(@"requestAddr%@",requestAddr);
  41. [request setURL:[NSURL URLWithString:requestAddr]];
  42. [request setHTTPMethod:@"GET"];
  43. [request setTimeoutInterval:10];
  44. NSURLConnection *conn = [[[NSURLConnection alloc]initWithRequest:request delegate:self] autorelease];
  45. if(conn)
  46. {
  47. receivedData = [[NSMutableData data] retain];
  48. while(!finished)
  49. {
  50. timeout ++;
  51. NSLog(@"outtime:%d",timeout);
  52. if(timeout>5)
  53. {
  54. finished = YES;
  55. }
  56. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1]];
  57. }
  58. }
  59. else
  60. {
  61. NSLog(@"失败");
  62. }
  63. }
  64. /*
  65. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  66. {
  67. finished = YES;
  68. //etrafficAppDelegate *app =[[UIApplication sharedApplication] delegate];
  69. //app.moudle.netWorkComplite = NO;
  70. NSLog(@"connection error");
  71. }
  72. */
  73.  
  74. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  75. {
  76. [receivedData appendData:data];
  77. NSLog(@"receivedDate:%@",receivedData);
  78. }
  79. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  80. {
  81. EpayLogin *epayloginxml;
  82. switch (curType)
  83. {
  84. case EPAYLOGIN:
  85. NSLog(@"sssssss");
  86. epayloginxml = [[EpayLogin alloc]initWithData:receivedData];
  87. NSMutableArray *aa = [[NSMutableArray alloc]init];
  88. aa = [epayloginxml beginParser];
  89. NSDictionary *a = [[NSDictionary alloc]init];
  90. a = [aa objectAtIndex:0];
  91. NSLog(@"aaaaaaaaaa:%@",[aa objectAtIndex:0]);
  92. NSLog(@"aa%@",[a objectForKey:@"Transreturn"]);
  93. LoginJudge *test = [[LoginJudge alloc]init];
  94. test.loginarray = aa;
  95. self.loginJudge =[test autorelease];
  96. NSLog(@"retainCount:%d",[self.loginJudge retainCount]);
  97. NSLog(@"loginarray:%@",[self.loginJudge.loginarray objectAtIndex:0]);
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. @end
  1. @class LoginJudge;
  2. #import <UIKit/UIKit.h>
  3. #import <zlib.h>
  4. #import "ParserForMainReturn.h"
  5. #import "ViewController.h"
  6. #import "EpayLogin.h"
  7. #import "LoginJudge.h"
  8.  
  9. enum _HttpRequestType
  10. {
  11. EPAYLOGIN,};
  12. typedef NSUInteger HTTPREQUESTTYPE;
  13. @interface Http : NSObject
  14. {
  15. NSMutableData *receivedData;
  16. NSDictionary *paramDict;
  17. BOOL finished;
  18. HTTPREQUESTTYPE curType;
  19. int timeout;
  20. }
  21. @property(nonatomic,retain)NSString *requestAddr;
  22. @property(nonatomic,retain)LoginJudge *loginJudge;
  23. -(id)initWithRequestStyle:(HTTPREQUESTTYPE)type paramArray:(NSDictionary*)params;
  24. -(NSString *)switchHttpUrlRequest:(HTTPREQUESTTYPE)type;
  25. -(void)requestAndconnect;
  26. @end
  1. #import "ParserForMainReturn.h"
  2. #import "Utilities.h"
  3. #import "NSDataGZipAdditions.h"
  4. #import <zlib.h>
  5. @implementation ParserForMainReturn
  6. @synthesize data;
  7. -(id)initWithData:(NSData*)pdata
  8. {
  9. if (!(self=[super init])) return nil;
  10. if (self) {
  11. self.data = pdata;
  12. mStr = [[NSMutableString alloc]init ];
  13. }
  14. return self;
  15. }
  16. -(NSData*)beginParser
  17. {
  18. parser = [[NSXMLParser alloc] initWithData:data];
  19. [parser setDelegate:self];
  20. [parser setShouldProcessNamespaces:YES];
  21. // The parser calls methods in this class
  22. [parser parse];
  23. [parser release];
  24. return mData;
  25. }
  26. - (void)parser:(NSXMLParser *)parser
  27. didStartElement:(NSString *)elementName
  28. namespaceURI:(NSString *)namespaceURI
  29. qualifiedName:(NSString *)qName
  30. attributes:(NSDictionary *)attributeDict
  31. {
  32. if ([elementName isEqualToString:@"return"]) {
  33. mainElement = MRETURN;
  34. return;
  35. }
  36. mainElement = 99;
  37. }
  38. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  39. {
  40. switch (mainElement) {
  41. case MRETURN:
  42. [mStr appendFormat:@"%@",string ];
  43. NSLog(@"mStr:%@",mStr);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. - (void)parser:(NSXMLParser *)parser
  50. didEndElement:(NSString *)elementName
  51. namespaceURI:(NSString *)namespaceURI
  52. qualifiedName:(NSString *)qName
  53. {
  54. if ([elementName isEqualToString:@"return"]) {
  55.  
  56. return;
  57. }
  58. }
  59. - (void)parserDidEndDocument:(NSXMLParser *)parser
  60. {
  61. NSData *unBase64 = [Utilities base64toByte:mStr];
  62. //NSData *unBase64 = [Utilities base64toByte: retStr];
  63. NSLog(@"len %d",[unBase64 length]);
  64.  
  65. if ([unBase64 length] == 0) {
  66. return;
  67. }
  68. //mData = [NSDataGZipAdditions dataWithCompressedBytes:[unBase64 bytes] length:[unBase64 length]];
  69. mData = [self uncompressZippedData:unBase64];
  70. NSLog(@"mData %@",[NSString stringWithUTF8String:[mData bytes]]);
  71. }
  72. -(NSData *)uncompressZippedData:(NSData *)compressedData
  73. {
  74. if ([compressedData length] == 0) return compressedData;
  75. unsigned full_length = [compressedData length];
  76. unsigned half_length = [compressedData length] / 2;
  77. NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
  78. BOOL done = NO;
  79. int status;
  80. z_stream strm;
  81. strm.next_in = (Bytef *)[compressedData bytes];
  82. strm.avail_in = [compressedData length];
  83. strm.total_out = 0;
  84. strm.zalloc = Z_NULL;
  85. strm.zfree = Z_NULL;
  86. if (inflateInit2(&strm,(15+32)) != Z_OK) return nil;
  87. while (!done) {
  88. // Make sure we have enough room and reset the lengths.
  89. if (strm.total_out >= [decompressed length]) {
  90. [decompressed increaseLengthBy: half_length];
  91. }
  92. strm.next_out = [decompressed mutableBytes] + strm.total_out;
  93. strm.avail_out = [decompressed length] - strm.total_out;
  94. // Inflate another chunk.
  95. status = inflate (&strm,Z_SYNC_FLUSH);
  96. if (status == Z_STREAM_END) {
  97. done = YES;
  98. } else if (status != Z_OK) {
  99. break;
  100. }
  101. }
  102. if (inflateEnd (&strm) != Z_OK) return nil;
  103. // Set real length.
  104. if (done) {
  105. [decompressed setLength: strm.total_out];
  106. NSLog(@"Y len:%lu",strm.total_out);
  107. return decompressed;
  108. //return [NSData dataWithData: decompressed];
  109. } else {
  110. NSLog(@"no");
  111. return nil;
  112. }
  113. }
  114. -(void)dealloc
  115. {
  116. self.data = nil;
  117. // [countyList release];
  118. [mStr release];
  119. [super dealloc];
  120. }
  121. @end
  1. #import <Foundation/Foundation.h>
  2.  
  3. enum __mainElement {
  4. MRETURN
  5. };
  6. typedef NSUInteger _mainElement;
  7. @interface ParserForMainReturn : NSObject <NSXMLParserDelegate>{
  8. NSData *data;
  9. NSXMLParser *parser;
  10. _mainElement mainElement;
  11. NSData *mData;
  12. NSMutableString *mStr;
  13. // NSMutableArray *mainList;
  14. // NSMutableDictionary *mainInfo;
  15. }
  16. @property(nonatomic,retain)NSData *data;
  17. -(id)initWithData:(NSData*)pdata ;
  18. -(NSData*)beginParser;
  19. -(NSData *)uncompressZippedData:(NSData *)compressedData ;
  20. @end

猜你在找的XML相关文章