XML解析

    NSString *strGetMsgComefrom=@"<?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>"

    "<GetMsgComefrom xmlns="http://tempuri.org/" />"

    "</soap12:Body>"

    "</soap12:Envelope>";

    

    

    

    

    

    NSString *soapLength=[NSString stringWithFormat:@"%lu",(unsigned long)[strGetMsgComefrom  length]];

    AFHTTPRequestOperationManager *firstmanager=[AFHTTPRequestOperationManager manager];

    firstmanager.responseSerializer=[[AFHTTPResponseSerializer alloc]init];

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

    [firstmanager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];

    NSMutableURLRequest *request=[firstmanager.requestSerializer requestWithMethod:@"POST"

                                                                         URLString:@"http://192.168.2.214:86/webservice/WebService.asmx" parameters:nil

                                                                             error:nil];

    

    [request setHTTPBody:[strGetMsgComefrom  dataUsingEncoding:NSUTF8StringEncoding]];

    AFHTTPRequestOperation *operation=[firstmanager HTTPRequestOperationWithRequest:request

                                                                      success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                                                          NSString *response=[[NSString alloc]initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];

                                                                          [self initXml:response];

                                                                          NSLog(@"%@,%@",operation,response);

                                                                      }

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

                                                                          NSString *response=[[NSString alloc]initWithData:(NSData *) [operation responseObject]encoding:NSUTF8StringEncoding];

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

                                                                      }];

    

    

    

    [firstmanager.operationQueue addOperation:operation];

    

    

    

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)initXml:(NSString *)value{

    NSError *error = nil;

    XmlParseHelper *xmlParse = [[XmlParseHelper alloc] initWithData:value];

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"http://tempuri.org/",@"result", nil];

    XmlNode *node= [xmlParse selectSingleNode:@"//result:GetMsgComefromResponse/result:GetMsgComefromResult" namespaces:dict];

  //  NSString * str = [node Value];

//    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"value:%@",[node Value]);

//    GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:&error];

    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:[node Value] options:0 error:&error];

    NSArray *nodes = [doc nodesForXPath:@"//node" error:&error];

    

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

    for (GDataXMLElement *element in nodes)

    {

        NSString *sid=[[element attributeForName:@"sid"] stringValue];

      //  NSString *type=[[element attributeForName:@"type"] stringValue];

        NSString *name=[[element attributeForName:@"name"] stringValue];

        NSLog(@"==%@,==%@",sid,name);

    }

   

    

    

}

1.首先得包含该文件。

GDataXMLNode.h

GDataXMLNode.m

(google的那有)

2.在build phases 中包含

libxml2.dylib库。

3.build settings 设置 header search paths:

 ${SDK_DIR}/usr/include/libxml2

4.xml文件放在supporting files 文件下

原文地址:https://www.cnblogs.com/linximu/p/4642046.html