iOS实现xml的post传递,返回xml数据进行解析
2016-10-21 11:39
706 查看
1、xml的post传递,传参数进行拼接
[objc] viewplain 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] viewplain 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添加成功
[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
相关文章推荐
- iOS实现xml的post传递,返回xml数据进行解析
- iphone实现xml的post传递,并且接收数据
- Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- php 调用接口的post方法,还有解析返回的xml数据
- 使用AFNetworking框架进行post 请求解析返回得数据
- Android通过post请求发送一个xml,解析返回xml数据
- iphone实现xml的post传递,并且接收数据
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- WebService传递XML数据 C#DataSet操作XML 解析WebService返回的XML数据
- iPhone实现XML的POST传递并接收数据
- 一个完整的通过encoding/json信息传递格式,采用http.POST利用进行与服务器通信,并对返回结果进行解析处理的典型案例
- 使用HttpClient 发送get、post请求,及其解析xml返回数据
- WebService传递XML数据 C#DataSet操作XML 解析WebService返回的XML数据
- Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
- iphone实现xml的post传递,并且接收数据
- 解析HTTP POST方式请求的xml数据、本地XML文件数据, 并返回JSON字符串
- simpleXML方式解析XML,并对XML数据进行增删改查
- simpleXML方式解析XML,并对XML数据进行增删改查
- php获取通过http协议post提交过来xml数据及解析xml