iOS开发 - GDataXML&KissXML解析

GData 解析

NSString * url = @"http://10.0.8.8/sns/my/user_list.php?format=xml";
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseObject options:0 error:nil];
    //根节点 rootElement拿到根节点
    GDataXMLElement * rootEle = [doc rootElement];
    //拿某个节点的所有子节点
    NSArray * listArr = [rootEle elementsForName:@"user_list"];
    //因为user_list节点只有一个 所有数组里只有一个元素
    GDataXMLElement * listEle = listArr[0];
    NSArray * userArr = [listEle elementsForName:@"user"];
    //因为user节点不止一个 通过遍历拿到每个user节点
    for (GDataXMLElement * element in userArr) {
        //创建模型
        Model * model = [[Model alloc] init];
        NSArray * uidArr = [element elementsForName:@"uid"];
        GDataXMLElement * uid = uidArr[0];
        model.uid = [uid stringValue];
        model.username = [[element elementsForName:@"username"][0] stringValue];
        NSLog(@"%@ %@",model.username,model.uid);
    }
} failure:^(NSURLSessionDataTask *task, NSError *error) {}];

KissXML 解析

NSString * url = @"http://10.0.8.8/sns/my/user_list.php?format=xml";
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    DDXMLDocument * doc = [[DDXMLDocument alloc] initWithData:responseObject options:1 error:nil];
    DDXMLElement * rootEle = [doc rootElement];
    NSArray * arr = [rootEle elementsForName:@"user_list"];
    DDXMLElement * listEle = arr[0];
    NSArray * usersArr = [listEle elementsForName:@"user"];
    for (DDXMLElement * userEle in usersArr) {
        NSArray * uidArr = [userEle elementsForName:@"uid"];
        //DDXMLNode 属性类型
        //DDXMLElement 节点类型
        DDXMLNode * node = [userEle attributeForName:@"ID"];
    }
} failure:^(NSURLSessionDataTask *task, NSError *error) {}];
/*
//使用XPath语法去找节点
//绝对路径&&相对路径
NSArray * userArr = [doc nodesForXPath:@"/root/user_list" error:nil];
NSArray * userArr2 = [doc nodesForXPath:@"//user_list" error:nil];
 */

你可能感兴趣的:(iOS开发 - GDataXML&KissXML解析)