您的位置:首页 > 移动开发 > IOS开发

iOS实现xml的post传递,返回xml数据进行解析

2016-10-21 11:39 706 查看


1、xml的post传递,传参数进行拼接

[objc] view
plain copy

-(void) postxml:(NSString*)vendor version:(NSString*)version  

{  

      

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  

    [request setURL:[NSURL URLWithString:URLstr]];  

    [request setHTTPMethod:@"POST"];//声明请求为POST请求  

    //set headers  

    NSString *contentType = [NSString stringWithFormat:@"text/xml"];//Content-Type数据类型设置xml类型  

    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];  

    //create the body  

    NSMutableData *postBody = [NSMutableData data];  

    [postBody appendData:[[NSString stringWithFormat:@"<此处写根节点><此处写报文头节点><reqUserId>123456</reqUserId><authId>LLLL</authId><authPw>123456</authPw></此处写报文头节点>"] dataUsingEncoding:NSUTF8StringEncoding]];  

      

    //拼接参数字符串  

    [postBody appendData:[[NSString stringWithFormat:@"<此处写报文体节点><deviceCode>"] dataUsingEncoding:NSUTF8StringEncoding]];  

    //vendor拼接在<deviceCode></deviceCode>标签对中间  

    [postBody appendData:[vendor dataUsingEncoding:NSUTF8StringEncoding]];  

    [postBody appendData:[[NSString stringWithFormat:@"</deviceCode><appId>FaceQualityCheck</appId><operateSystem>iOS</operateSystem><operateSystemVersion>"] dataUsingEncoding:NSUTF8StringEncoding]];  

    [postBody appendData:[version dataUsingEncoding:NSUTF8StringEncoding]];  

    [postBody appendData:[[NSString stringWithFormat:@"</operateSystemVersion></此处写报文体节点></此处写根节点>"] dataUsingEncoding:NSUTF8StringEncoding]];  

      

    [request setHTTPBody:postBody];  

      

    NSString *bodyStr = [[NSString alloc] initWithData:postBody  encoding:NSUTF8StringEncoding];  

    NSLog(@"bodyStr: %@ ",bodyStr);  

      

    //get response  

    NSHTTPURLResponse* urlResponse = nil;  

    NSError *error = [[NSError alloc] init];  

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  

    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  

    NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]);  

    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {  

        NSLog(@"Response: %@", result);  

    }  

}  


2、xml的post传递,不传参

[objc] view
plain copy

-(void) postxml  

{  

      

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  

    [request setURL:[NSURL URLWithString:URLstr]];  

    [request setHTTPMethod:@"POST"];//声明请求为POST请求  

    //set headers  

    NSString *contentType = [NSString stringWithFormat:@"text/xml"];//Content-Type数据类型设置xml类型  

    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];  

    //create the body  

    NSMutableData *postBody = [NSMutableData data];  

    [postBody appendData:[[NSString stringWithFormat:@"<此处写根节点><此处写报文头节点><reqUserId>123456</reqUserId><authId>LLLL</authId><authPw>123456</authPw></此处写报文头节点>"] dataUsingEncoding:NSUTF8StringEncoding]];  

      

    [postBody appendData:[[NSString stringWithFormat:@"<此处写报文体节点><deviceCode>2223222</deviceCode><appId>FaceQualityCheck</appId><operateSystem>iOS</operateSystem><operateSystemVersion>8.3</operateSystemVersion></此处写报文体节点></此处写根节点>"] dataUsingEncoding:NSUTF8StringEncoding]];  

      

    [request setHTTPBody:postBody];  

      

    NSString *bodyStr = [[NSString alloc] initWithData:postBody  encoding:NSUTF8StringEncoding];  

    NSLog(@"bodyStr: %@ ",bodyStr);  

      

    //get response  

    NSHTTPURLResponse* urlResponse = nil;  

    NSError *error = [[NSError alloc] init];  

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  

    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  

    NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]);  

    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {  

        NSLog(@"Response: %@", result);  

    }  

}  


3、xml解析

1.网上搜索GDataXMLNode,下载得到GDataXMLNode.h和GDataXMLNode.m,导入工程。

2.再导入libxml2.tbd库。

3.在工程的“Build Settings”页中找到“Header Search Path”项,添加/usr/include/libxml2"到路径中

4.编译,如工程能编译通过,则说明GDataXMLNode添加成功


3.1解析xml文本

[objc] view
plain copy

NSString *xmlPath = [[NSBundlemainBundle] pathForResource:@"test"ofType:@"xml"];  

NSString *xmlString = [NSStringstringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncodingerror:nil];  

GDataXMLDocument *xmlDoc = [[GDataXMLDocumentalloc] initWithXMLString:xmlString options:0 error:nil];  

GDataXMLElement *xmlEle = [xmlDoc rootElement];  

NSArray *array = [xmlEle children];  

NSLog(@"count : %d", [array count]);  

  

for (int i = 0; i < [array count]; i++) {  

    GDataXMLElement *ele = [array objectAtIndex:i];  

      

    // 根据标签名判断  

    if ([[ele name] isEqualToString:@"name"]) {  

        // 读标签里面的属性  

        NSLog(@"name --> %@", [[ele attributeForName:@"value"] stringValue]);  

    } else {  

        // 直接读标签间的String  

        NSLog(@"age --> %@", [ele stringValue]);  

    }  

      

}  

[objc] view
plain copy

}  

3.2解析返回数据,

[objc] view
plain copy

responseData是返回的data  

[objc] view
plain copy

GDataXMLDocument *document=[[GDataXMLDocument alloc]initWithData:responseData options:kNilOptions error:nil];  

GDataXMLElement *root=document.rootElement;  

NSArray *arr1=root.children;  

for (int i = 0; i < [arr1 count]; i++) {  

    GDataXMLElement *ele = [array objectAtIndex:i];  

      

    // 根据标签名判断  

    if ([[ele name] isEqualToString:@"name"]) {  

        // 读标签里面的属性  

        NSLog(@"name --> %@", [[ele attributeForName:@"value"] stringValue]);  

    } else {  

        // 直接读标签间的String  

        NSLog(@"age --> %@", [ele stringValue]);  

    }  

      



XMLWriter    https://github.com/ahmyi/XMLWriter

XMLReader   https://github.com/amarcadet/XMLReader
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml iOS
相关文章推荐