#import <UIKit/UIKit.h> #import "GDataXMLNode.h" @interface ZYKAppDelegate : UIResponder <UIApplicationDelegate> { //声明xml文档解析成员变量 GDataXMLDocument *_doc; } @property (strong, nonatomic) UIWindow *window; @end #import "ZYKAppDelegate.h" @implementation ZYKAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. [self xmlXpathParser2]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } //逐层解析 -(void)xmlParser { NSString *strURL=[[NSBundle mainBundle]pathForResource:@"xml" ofType:@"txt"]; NSString *strContent=[[NSString alloc]initWithContentsOfFile:strURL encoding:NSUTF8StringEncoding error:nil]; //实例化 _doc=[[GDataXMLDocument alloc]initWithXMLString:strContent options:0 error:nil]; GDataXMLElement *rootElement=[_doc rootElement]; //在跟节点里边查找books子元素 NSArray *booksArray=[rootElement elementsForName:@"books"]; //从数组中获得books子元素 GDataXMLElement *booksElement=[booksArray objectAtIndex:0]; //在books节点里边查找book子元素 NSArray *bookArray=[booksElement elementsForName:@"book"]; //从数组中获得第二个book元素 GDataXMLElement *secondBookElement=[bookArray objectAtIndex:1]; //在第二个book元素中查找name自元素 NSArray *nameArray=[secondBookElement elementsForName:@"name"]; GDataXMLElement *nameElement=[nameArray objectAtIndex:0]; NSLog(@"%@",nameElement.stringValue); } //xPath解析 -(void)xmlXpathParser { NSString *strURL=[[NSBundle mainBundle]pathForResource:@"xml" ofType:@"txt"]; //将路径下文件得内容加载到字符串中 NSString *strContent=[[NSString alloc]initWithContentsOfFile:strURL encoding:NSUTF8StringEncoding error:nil]; _doc=[[GDataXMLDocument alloc]initWithXMLString:strContent options:0 error:nil]; //获得文档得根节点 GDataXMLElement *rootElement=[_doc rootElement]; //查找制定路径下得节点 //xPath目录必须从根路径开始,而且不能层次跳跃 //通过xPath方法查找得元素为xPath路径尾部(@"/根节点/子节点/.../尾节点")的元素 NSArray *array= [rootElement nodesForXPath:@"/root/books/book" error:nil]; GDataXMLElement *secondBookElement=[array objectAtIndex:1]; //在第二个book元素中查找name子元素 NSArray *nameArray=[secondBookElement elementsForName:@"name"]; GDataXMLElement *nameElement=[nameArray objectAtIndex:0]; //获得name元素的值 NSLog(@"%@",nameElement.stringValue); } -(void)xmlXpathParser2 { NSString *strURL=[[NSBundle mainBundle]pathForResource:@"xml" ofType:@"txt"]; //将路径下文件得内容加载到字符串中 NSString *strContent=[[NSString alloc]initWithContentsOfFile:strURL encoding:NSUTF8StringEncoding error:nil]; _doc=[[GDataXMLDocument alloc]initWithXMLString:strContent options:0 error:nil]; //获得文档得根节点 GDataXMLElement *rootElement=[_doc rootElement]; //如果xPath路径为//节点,那么文件中所有的该节点都会被查找到 NSArray *array=[rootElement nodesForXPath:@"//name" error:nil]; for (GDataXMLElement *nameElement in array) { NSLog(@"%@",nameElement.stringValue); } }
#import <UIKit/UIKit.h> #import "ASIFormDataRequest.h" @interface FirstViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,ASIHTTPRequestDelegate> { //声明相册控制器 UIImagePickerController *_pickerController; //可以上传图片和字符串 ASIFormDataRequest *_request; UIImage *image; } @property (weak, nonatomic) IBOutlet UIImageView *imgView; - (IBAction)btnClick:(id)sender; - (IBAction)upLoadClick:(id)sender; - (IBAction)loginBtnClick:(id)sender; @property (weak, nonatomic) IBOutlet UITextField *nameTextField; @property (weak, nonatomic) IBOutlet UITextField *pwdTextField; @end #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (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. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)btnClick:(id)sender { //实例化相册 _pickerController=[[UIImagePickerController alloc]init]; _pickerController.delegate=self; [self presentViewController:_pickerController animated:YES completion:^{ }]; } - (IBAction)upLoadClick:(id)sender { NSURL *url=[NSURL URLWithString:@"http://10.0.8.8/sns/my/upload_photo.php"]; _request=[[ASIFormDataRequest alloc]initWithURL:url]; _request.delegate=self; //设置上传方式(post比get传输更安全) [_request setRequestMethod:@"post"]; //将图片对象转换成NSData数据 NSData *data=UIImagePNGRepresentation(image); //第一个参数为准备上传图片的data数据 //第二为上传到服务器后,该图片的名称 //第三个参数为图片类型 //第四个参数为服务器接收图片的key [_request setData:data withFileName:@"a.png" andContentType:@"image/png" forKey:@"attach"]; //开始上传图片 [_request startAsynchronous]; } //登录调用 - (IBAction)loginBtnClick:(id)sender { // NSURL *url=[NSURL URLWithString:@"http://10.0.8.8/sns/my/login.php"]; _request=[[ASIFormDataRequest alloc]initWithURL:url]; [_request setRequestMethod:@"post"]; [_request addPostValue:self.nameTextField.text forKey:@"username"]; [_request addPostValue:self.pwdTextField.text forKey:@"password"]; _request.delegate=self; [_request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { //相应成功调用 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dict); } - (void)requestFailed:(ASIHTTPRequest *)request { NSLog(@"数据失败"); } //选择相册中图片时调用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"abc"); //获得选中的图片 image=[info objectForKey:@"UIImagePickerControllerOriginalImage"]; self.imgView.image=image; [_pickerController dismissViewControllerAnimated:YES completion:^{ }]; } @end
#import <UIKit/UIKit.h> #import "AFNetworking.h" #import "GDataXMLNode.h" @interface ZYKAppDelegate : UIResponder <UIApplicationDelegate> { AFHTTPRequestOperationManager *_manager; GDataXMLDocument *_doc; } @property (strong, nonatomic) UIWindow *window; @end #import "ZYKAppDelegate.h" @implementation ZYKAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. _manager=[AFHTTPRequestOperationManager manager]; //关闭AF自动解析功能 _manager.responseSerializer=[AFHTTPResponseSerializer serializer]; //加载数据 [_manager GET:@"http://10.0.8.8/sns/my/user_list.php?page=1&number=5&format=xml" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //如果相应成功,加载数据 //通过GData框架,手动解析加载过来的数据 _doc =[[GDataXMLDocument alloc]initWithData:responseObject options:0 error:nil]; GDataXMLElement *rootElement=[_doc rootElement]; NSLog(@"%@",rootElement); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }