基于SOAP的xml网络交互心得

来源:互联网 发布:高维空间存在吗 知乎 编辑:程序博客网 时间:2024/05/26 12:07

感谢小二同学将遇到的问题分享给我们,再此给以掌声。如果看不懂下面文章的建议查找一下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


原创粉丝点击