iOS使用GData对webService进行解析xml

1)GDataXMLNode说明

GDataXMLNode是Google提供的用于XML数据处理的类集。该类集对libxml2--DOM处理方式进行了封装,能对较小或中等的xml文档进行读写操作且支持XPath语法。

使用方法:
     1、获取GDataXMLNode.h/m文件,将GDataXMLNode.h/m文件添加到工程中
     2、向工程中增加“libxml2.dylib”库
     3、 在工程的“Build Settings”页中找到“Header Search Path”项,添加/usr/include/libxml2"到路径中
     4、 搜索Other linker flags,同样的方式添加-lxml2
     5、添加“GDataXMLNode.h”文件到头文件中,如工程能编译通过,则说明GDataXMLNode添加成功
2)使用示例
    webService的XML
POST /WebService.asmx HTTP/1.1
Host: 139.196.32.138
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <TransformData xmlns="http://tempuri.org/">
      <str_json>string</str_json>
    </TransformData>
  </soap12:Body>
</soap12:Envelope>
 解析代码如下:

//------ 拼接SOAP协议 ------

    NSDictionary *dataDictionary = [NSDictionarydictionaryWithObjectsAndKeys:

                                   @"888888",@"TaskGuid",

                                    @"888888",@"DataType",

                                    @"888888",@"LoginAccount",

                                    @"888888",@"Password",nil];

    NSError *error =nil;

    NSData *jsonData = [NSJSONSerializationdataWithJSONObject:dataDictionary

                                                      options:NSJSONWritingPrettyPrinted

                                                        error:&error];

    NSString *jsonString = [[NSStringalloc]initWithData:jsonData

                                                 encoding:NSUTF8StringEncoding];

    NSString *soapMessage = [NSStringstringWithFormat:

                            @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"

                            "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"

                             "<soap12:Body>"

                            "<TransformData xmlns=\"http://tempuri.org/\">"

                             "<str_json>%@</str_json>"

                             "</TransformData>"

                             "</soap12:Body>"

                             "</soap12:Envelope>",jsonString];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];

    manager.responseSerializer = [[AFHTTPResponseSerializeralloc]init];

    [manager.requestSerializersetValue:@"139.196.32.138"forHTTPHeaderField:@"Host"];

    [manager.requestSerializersetValue:@"application/soap+xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];

    [manager.requestSerializersetValue:[NSStringstringWithFormat:@"%d",(int)soapMessage.length]forHTTPHeaderField:@"Content-Length"];

    NSMutableURLRequest *request = [manager.requestSerializerrequestWithMethod:@"POST"URLString:@"http://139.196.32.138:10011/WebService.asmx?op=TransformData"parameters:nilerror:nil];

    [request setHTTPBody:[soapMessagedataUsingEncoding:NSUTF8StringEncoding]];

    AFHTTPRequestOperation *operation = [managerHTTPRequestOperationWithRequest:requestsuccess:^(AFHTTPRequestOperation *operation,id responseObject) {

        NSString *xmlString = [[NSStringalloc]initWithData:(NSData *)[operationresponseObject]encoding:NSUTF8StringEncoding];

        //使用NSData对象初始化 

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

        //获取根节点  

        GDataXMLElement *xmlEle = [xmlDocrootElement];

        NSArray *array = [xmlElechildren];

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

            GDataXMLElement *ele = [arrayobjectAtIndex:i];

            NSLog(@"%@",[elestringValue]);

           [ele stringValue]是解析结果,可以进一步转换成json对象

       }

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

        NSLog(@"===%@",error);

    }];

    [manager.operationQueueaddOperation:operation];

   




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值