objective-c Http xml 文件解压 解析。NSXMLParser

吴修洁
2023-12-01


//
//  setting.h
//  TestNew
//
//  Created by jlz on 12-6-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ViewController.h"
#import <zlib.h>



typedef enum _testElement 
{
    one,
    two,
    three,
    four,
    
}_testElement;



@interface setting : UIViewController<UITextFieldDelegate,NSXMLParserDelegate>{
    NSMutableData *_receivedData;
    BOOL _recving;
    NSXMLParser *testParser;
    NSMutableDictionary *navigateLineInfo;
    NSMutableDictionary *navigatePoiInfo;
    NSMutableArray *redxmlinformation;
    _testElement testElement;
}

- (IBAction)saveSet:(id)sender; 
- (IBAction)tohomeSet:(id)sender;  //回到主页
- (IBAction)testHttpXml:(id)sender; //测试XML
-(void)addbutton;
-(NSMutableDictionary *)beginParser;
- (void)bPress:(id)sender;
-(void)redfile;
-(void)writefile;
@property (retain, nonatomic) IBOutlet UITextField *serveraddr;
@property (retain, nonatomic) IBOutlet UITextField *Customeradd;
@property (retain, nonatomic) IBOutlet UITextField *httpXmltxt;
@property (retain, nonatomic) IBOutletCollection(UITextView) NSArray *displayHttpXml;

@end
 
  
 
  







// // setting.m // TestNew // // Created by jlz on 12-6-29. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "setting.h" #import "ParserForMainReturn.h" @interface setting () @end @implementation setting @synthesize serveraddr; @synthesize Customeradd; @synthesize httpXmltxt; @synthesize displayHttpXml; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [serveraddr setDelegate:self]; [Customeradd setDelegate:self]; [httpXmltxt setDelegate:self]; NSLog(@"server setting...\n"); [self redfile]; _recving = NO; } - (void)viewDidUnload { [self setServeraddr:nil]; [self setCustomeradd:nil]; [self setHttpXmltxt:nil]; [self setDisplayHttpXml:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (IBAction)saveSet:(id)sender { NSLog(@"save now ...\n"); //NSString *server_addvarle = [sender titleForState:UIControlStateNormal]; //NSString *serveradd = [[NSString alloc]initWithFormat:@"%@",server_addvarle]; NSLog(@"serveraddr:%@",serveraddr.text); NSLog(@"Customeraddco:%@",Customeradd.text); httpXmltxt.text = @"http://115.238.143.98/services/AccountWebServiceGZIP/getlogin?LoginIdType=2&LoginId=F9F6B693-F909-5809-8CE3-17CB17CC1C5E&PhoneSystem=2&CurrentVerson=v1.02"; NSLog(@"xmlHttptxt:%@",httpXmltxt.text); if([serveraddr.text isEqualToString:@"aaa"]) { NSLog(@"the same\n"); } [self writefile]; } -(void)writefile { /* FILE *fp=fopen([[self bundlePath:@"serveradd.txt"]UTF8String], "w"); char str[100]= "addr:192.168.1.1"; if (!fp) { return; } fwrite(str, sizeof(uint8_t), sizeof(str), fp); fclose(fp); */ NSArray *serverinfomation = [NSArray arrayWithObjects:self.serveraddr.text,self.Customeradd.text, nil]; [serverinfomation writeToFile:[self documentsPath:@"server.txt"] atomically:YES]; NSArray *httpxmltest = [NSArray arrayWithObjects:self.httpXmltxt.text, nil]; [httpxmltest writeToFile:[self documentsPath:@"httpXmlfile.txt" ]atomically:YES]; } -(void)redfile //读文件 { FILE *fp=fopen([[self bundlePath:@"serveradd.txt"]UTF8String], "r"); char word[100]; if (!fp) { return; } while(fgets(word, 100,fp)) { word[strlen(word)-1] = '\0'; NSLog(@"%s",word); } fclose(fp); NSString *filePath = [self documentsPath:@"server.txt"]; //从filePath 这个指定的文件里读 NSArray *serverinfomation = [NSArray arrayWithContentsOfFile:filePath]; // NSLog(@"%@",[serverinfomation objectAtIndex:1] ); self.serveraddr.text = [serverinfomation objectAtIndex:0]; self.Customeradd.text = [serverinfomation objectAtIndex:1]; NSString *redHttpXmlPath = [self documentsPath:@"httpXmlfile.txt"]; NSArray * redHttpXmlFile = [NSArray arrayWithContentsOfFile:redHttpXmlPath]; self.httpXmltxt.text = [redHttpXmlFile objectAtIndex:0]; } //文件路 -(NSString *)bundlePath:(NSString *)fileName { return [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:fileName]; } -(NSString *)documentsPath:(NSString *)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:fileName]; } -(NSString *)documentsPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return documentsDirectory; } - (IBAction)tohomeSet:(id)sender { [self.view removeFromSuperview]; } -(IBAction)testHttpXml:(id)sender { NSLog(@"httpXml...."); //[self addbutton]; NSMutableString *addrXml = [[[NSMutableString alloc]initWithString: httpXmltxt.text]autorelease]; NSMutableURLRequest *requestXml = [[[NSMutableURLRequest alloc] init] autorelease]; _receivedData = [[NSMutableData alloc]init]; [addrXml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [requestXml setURL:[NSURL URLWithString:addrXml]]; [requestXml setHTTPMethod:@"GET"]; [requestXml setTimeoutInterval:10]; NSURLConnection *xmlconnet = [[[NSURLConnection alloc] initWithRequest:requestXml delegate:self]autorelease]; if(xmlconnet) { _recving = YES; } while (_recving) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1]]; } //添加按钮 [self addbutton]; //格式转化 // NSLog(@"%@",[[[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding]autorelease] ); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_receivedData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { _recving = NO; ParserForMainReturn *testParser2 = [[ParserForMainReturn alloc]initWithData:_receivedData]; _receivedData = [testParser2 beginParser]; // nsstring *testParser2 = [[setting alloc]initWithData:[testParser2 beginParser]]; //NSString *testParser2 = [[NSString alloc]init]; // NSString *testdata = [[NSString alloc]initWithData: encoding:NSUTF8StringEncoding]; NSLog(@"XXXX%@",[self beginParser]); } -(NSMutableDictionary *)beginParser { navigatePoiInfo = [[NSMutableDictionary alloc]init]; testParser = [[NSXMLParser alloc]initWithData: _receivedData]; [testParser setDelegate:self]; [testParser setShouldProcessNamespaces:YES]; // The parser calls methods in this class [testParser parse]; [testParser release]; return [navigateLineInfo autorelease]; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"return"]) { testElement = 6; redxmlinformation = [[NSMutableArray alloc]init]; return; } if ([elementName isEqualToString:@"loginStatue"]) { testElement = one; return; } if ([elementName isEqualToString:@"loginContent"]) { testElement = two; return; } if ([elementName isEqualToString:@"recommandContent"]) { testElement = three; return; } testElement = 6; } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { switch (testElement) { case one: [navigatePoiInfo setObject:string forKey:@"loginStatue"]; [redxmlinformation addObject:string]; NSLog(@"start : %@",string ); break; case two: [navigatePoiInfo setObject:string forKey:@"loginContent"]; [redxmlinformation addObject:string]; NSLog(@"resoult : %@",string ); break; case three: [navigatePoiInfo setObject:string forKey:@"recommandContent"]; [redxmlinformation addObject:string]; NSLog(@"infomation : %@",string ); break; default: break; } //[navigatePoiInfo setObject:string forKey:@"recommandContent"]; //id ontest = [navigatePoiInfo objectForKey:@"recommandContent"]; //NSLog(@"ontest%@",ontest); } - (void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"redxmlinformation %@",redxmlinformation); // [redxmlinformation release]; } //添加按钮 -(void)addbutton { NSLog(@"begin addbutton"); UIScrollView *testView = [[UIScrollView alloc]init]; testView.frame = CGRectMake(44, 165, 216, 240); testView.backgroundColor = [UIColor redColor]; [self.view addSubview:testView]; UIScrollView *secview = [[UIScrollView alloc]init]; secview.frame = CGRectMake(0, 0, 216, 240); secview.backgroundColor = [UIColor yellowColor]; [testView addSubview:secview]; int i; int n = 0; for(i = 0; i<[redxmlinformation count] ; i++) { UIButton *testbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [testbutton setTitle:[redxmlinformation objectAtIndex:i] forState:UIControlStateNormal]; //[buttonview setTitle:[userinfo objectAtIndex:i] forState:UIControlStateNormal]; testbutton.frame = CGRectMake(10, 10+n, 188, 25); [secview addSubview:testbutton]; n = n+30; } [secview setContentSize:CGSizeMake(216, 400)]; secview.showsHorizontalScrollIndicator = YES; secview.canCancelContentTouches = NO; } /* - (NSMutableData *)replaceHtmlEntities:(NSMutableData *)data1 { NSLog(@"data....."); NSString *htmlCode = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding]; NSMutableString *temp = [NSMutableString stringWithString:htmlCode]; [temp replaceOccurrencesOfString:@"&amp;" withString:@"#" options:NSLiteralSearch range:NSMakeRange(0, [temp length])]; NSData *finalData = [temp dataUsingEncoding:NSUTF8StringEncoding]; [data1 setData:finalData]; [htmlCode release]; return data1; } */ /* NSMutableDictionary *d = [NSMutableDictionary dictionary]; [d setObject:@"qingjoin" forKey:@"serveraddr"]; [d setObject:@"pwdpwd" forKey:@"userpwd"]; NSLog(@"%@",d); //新建一个对象。 id ontest = [d objectForKey:@"serveraddr"]; id sectest = [d objectForKey:@"userpwd"]; NSLog(@"%@",ontest); NSLog(@"%@",sectest); */ - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom]; [b setFrame:CGRectMake(0, 0, 320, 460)]; [self.view addSubview:b]; [b addTarget:self action:@selector(bPress:) forControlEvents:UIControlEventTouchUpInside]; return YES; } -(void)bPress:(id)sender { [sender removeFromSuperview]; [serveraddr resignFirstResponder]; [Customeradd resignFirstResponder]; [httpXmltxt resignFirstResponder]; } -(void)dealloc { NSLog(@"settung close"); [serveraddr release]; [serveraddr release]; [Customeradd release]; [httpXmltxt release]; [displayHttpXml release]; [super dealloc]; } @end

转载于:https://www.cnblogs.com/qingjoin/archive/2012/07/12/2588425.html

 类似资料: