加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

基于SOAP的xml网络交互心得

发布时间:2020-12-16 05:19:02 所属栏目:百科 来源:网络整理
导读:感谢 小二 同学将遇到的问题分享给我们,再此给以掌声。如果看不懂下面文章的建议查找一下HTTP协议的文艺,对HTTP协议要有个概念。 XML网络交互心得 目录 一、xml解析 1、根路径下 2、some路径下 二、xml soap请求 一、xml解析(例:如下图xml返回值) 首先

感谢小二同学将遇到的问题分享给我们,再此给以掌声。如果看不懂下面文章的建议查找一下HTTP协议的文艺,对HTTP协议要有个概念。

XML网络交互心得


目录

一、xml解析

1、根路径下

2、some路径下

二、xml soap请求

一、xml解析(例:如下图xml返回值)

首先下载GDataXMLNode库,安装见网络教程

1、根路径下

例:<res>

.m文件中

#import” GDataXMLNode”

NSString *soapMsg = [NSStringstringWithFormat:
                            
                             @"<?xmlversion="1.0" encoding="utf-8"?>n"//通用
                            
                             "<soap:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n"//通用
                            
                             "<soap:Body>n"//通用
                            
                             "<someThingToDoxmlns="http://xxx.xxx.xxx/">n"//注释1
                        
                            "<xxxx>%@</xxxx>n"
                            "<xxxx>%@</xxxx>n"
                           "<xxxx>%@</xxxx>n"//注释2
                             "</someThingToDo>n"
                            
                            "</soap:Body>n"
                            
                            "</soap:Envelope>n",@"A106",@"A10602",[NSStringstringWithFormat:@"<?xml version="1.0"encoding="utf-8"?><root><loginname>%@</loginname><loginpwd>%@</loginpwd><hqid>%@</hqid></root>",@"13811110001",@"123456",@"94d1a83e961f4fbf86b2291c502f1a11"]];

//判断是否成功


if (doc)
    {
       NSArray *images = [doc nodesForXPath:@"//res" error:nil];//关键:用xpath的//方法找到res的相对路径
       NSLog(@"%@",images);//打印一下,越打越安心
       for(GDataXMLElement *ppp in images)//找到images中的元素
       {
           NSLog(@"%@",ppp.stringValue);//打印
       }
}


2、some路径下

例:<row>

.m文件中

#import” GDataXMLNode”
NSData *xmlData = [[NSData alloc]initWithContentsOfFile:str];//同上
   
   GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlDataoptions:0 error:nil]; //同上
   if (doc)
   {       
       NSArray *addr = [doc nodesForXPath:@"//row" error:nil]; //同上
       for(GDataXMLElement *temp in addr) //同上
       {
           NSLog(@"%@",temp); //同上
           for(GDataXMLElement *here in temp.children) //因为在row的下面还有一层,所以这里找row的孩子
           {
                NSLog(@"here is%@",here.stringValue);//同上
           }
       }
}


二、xml soap请求

webservice的服务器是不能接收浏览器gei方法或者直接setvalue的,他需要拼出符合xml规则的soap字符串

直接代码举例

//注释3

NSLog(@"%@",soapMsg); //越打越放心

// 创建URL,内容是前面的请求报文报文中第二行主机地址加上第一行URL字段

NSURL *url = [NSURL URLWithString: @"http://xxx.xxx/xxx/xxx/"];//注释4

// 根据上面的URL创建一个请求

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];//通用

NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];//通用

// 添加请求的详细信息,与请求报文前半部分的各字段对应

[req addValue:@"text+xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];//通用
   [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];//通用
   [req addValue: @" http://xxx.xxx.xxx/ someThingToDo "forHTTPHeaderField:@"SOAPAction"]; //注释5

// 设置请求行方法为POST,与请求报文第一行对应

[req setHTTPMethod:@"POST"];//通用

// 将SOAP消息加到请求中

[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; //通用

// 创建连接

NSURLResponse *reponse; //通用
   NSError *error = nil; //通用
   NSData *responseData = [NSURLConnection sendSynchronousRequest:reqreturningResponse:&reponse error:&error]; //返回值
   if(responseData)
    {
       NSString *responseString = [[NSString alloc] initWithData:responseDataencoding:NSUTF8StringEncoding]; //把返回值转成string
       NSLog(@"%@",responseString); //打印出来检查一下,很开心
    }

注释1:<someThingToDo xmlns="http://xxx.xxx.xxx/">n"这里的someThingToDo是webservice的方法名,我们会通过这个方法访问服务器,不然访问不到;http://xxx.xxx.xxx/是这个方法的命名空间,注意这个不是服务器的url(重要)

注释2:这三行是注释1中方法的参数,前两个是普通str,第三个是xml的str(见注释3)

注释3:普通str可以直接%@加入,xml的str需要注意里面的”(需要用转义)和<(写为&lt;)、>(写为&gt;)尤其是<、>不按照括号里的写法的话,服务器是不认的

注释4:NSURL *url = [NSURL URLWithString: @"http://xxx.xxx/xxx/xxx/"];这个才是我们访问的url,要和注释1的命名空间区分

注释5:[req addValue: @" http://xxx.xxx.xxx/ someThingToDo "forHTTPHeaderField:@"SOAPAction"];很重要,这里添加我们的访问方法,这里的http://xxx.xxx.xxx/和someThingToDo都要与注释1的对应好~

到此为止,感谢大家对我的帮助,希望也能帮到大家。




QQ群号:241465868

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读