zoukankan      html  css  js  c++  java
  • 数据解析总结

    数据解析20161213

    XML解析:

    1、两层基础解析,对象里有对象

    xml结构

    Result

    /Result

    Message

    /Message

    loginmsg

    <soufunid>

    <soufunName>

    <RealName>

    /loginmsg

     头文件:

    @class SHDetailDataModel;
    
    @interface SHRegisterDesignerModel : NSObject
    
    @property(nonatomic,strong)NSString * Result;
    
    @property(nonatomic,strong)NSString * Mesage;
    
    @property(nonatomic,strong)SHDetailDataModel * detailDataModel;
    
     
    
    +(id)praseSHRegisterDesignerModel:(NSString *)str;
    
    @end
    
     
    
    @interface SHDetailDataModel : NSObject
    
    @property(nonatomic,strong)NSString *soufunid;
    
    @property(nonatomic,strong)NSString *soufunName;
    
    @property(nonatomic,strong)NSString *RealName;
    
    @property(nonatomic,strong)NSString *CityName;
    
    @property(nonatomic,strong)NSString *Email;
    
    @property(nonatomic,strong)NSString *logo;
    
    @property(nonatomic,strong)NSString *soufunmobile;
    
    @property(nonatomic,strong)NSString *CompanyName;
    
    @property(nonatomic,strong)NSString *Type;
    
    @end

    实现:

    @implementation SHRegisterDesignerModel
    
    +(id)praseSHRegisterDesignerModel:(NSString *)str{
    
        NSMutableArray * nodes = [[NSMutableArray alloc] init];
    
    //Root节点下的第一层数据  
    
      [nodes addObject:@"//Root”];
    
     
    
    //loginmsg节点下的数据
    
        [nodes addObject:@"//Root//loginmsg”];
    
     
    
        NSMutableArray * modes = [[NSMutableArray alloc] init];
    
    //Root节点下的第一层数据的model类  
    
      [modes addObject:@"SHRegisterDesignerModel"];
    
     
    
    //loginmsg节点下的数据model的类
    
        [modes addObject:@"SHDetailDataModel"];
    
     
    
        //nodes和modes的关系是一一对应的
    
     
    
        id iret = [GDataParseXML utilitParseDataFromXML:str nodeArrary:nodes modeArrary:modes andOpption:NO];
    
        return iret;
    
    }
    
    @end
    
     
    
    @implementation SHDetailDataModel
    
     
    
     
    
    @end

    解析具体实现方法:

    + (id)utilitParseDataFromXML:(NSString *)xmlString nodeArrary:(NSMutableArray*) nodes modeArrary:(NSMutableArray*) modes andOpption:(BOOL) isArray {
    
        NSError *error;
    
        GDataXMLDocumentAnother *doc = [[GDataXMLDocumentAnother alloc] initWithXMLString:xmlString options:0 error:&error];
    
        if (doc == nil) {
    
            return nil;
    
        }
    
        if ([nodes count] != [modes count]) {
    
            [doc release];
    
            return nil;
    
        }
    
        NSMutableArray * arrayForObjs = nil;
    
        if (isArray) {
    
            arrayForObjs = [[NSMutableArray alloc] init];
    
        }
    
        NSMutableArray *array = nil;
    
        NSMutableArray * propertyBase = [[NSMutableArray alloc] init];
    
        NSMutableDictionary *propertyDicBase =[[NSMutableDictionary alloc] init];
    
        
    
        id baseObj = [[NSClassFromString([modes objectAtIndex:0]) alloc] init];
    
        [GDataParseXML propertyMutableArray:propertyBase obj:baseObj];
    
        [GDataParseXML propertyDictionary:propertyDicBase obj:baseObj];
    
        
    
        int index =0;
    
        for (int i = 0;i< [nodes count] ;i++) {
    
            id obj = nil;
    
            NSMutableArray * propertyNames =nil;
    
            NSMutableDictionary *propertyDic =nil;
    
            if (i!= 0) {
    
                for (NSString* keystr in propertyBase) {
    
                    if((index == [propertyBase indexOfObject:keystr]) && [[propertyDicBase objectForKey:keystr] isEqualToString:@"NSMutableArray"]){
    
                        array = [[NSMutableArray alloc] init];
    
                    }
    
                    
    
                }
    
                if (array == nil) {
    
                    propertyNames = [[NSMutableArray alloc] init];
    
                    propertyDic =[[NSMutableDictionary alloc] init];
    
                    obj = [[NSClassFromString([modes objectAtIndex:i]) alloc] init];
    
                    [GDataParseXML propertyMutableArray:propertyNames obj:obj];
    
                    [GDataParseXML propertyDictionary:propertyDic obj:obj];
    
                    
    
                }
    
            }
    
            NSArray *base = [doc nodesForXPath:[nodes objectAtIndex:i] error:nil];
    
            if (![base count]) {
    
                [array release];
    
                [obj release];
    
                obj = nil;
    
                array = nil;
    
            }
    
            
    
            for (GDataXMLNodeAnother *thisObeject in base){
    
                if (obj == nil && i!=0 && !isArray) {
    
                    propertyNames = [[NSMutableArray alloc] init];
    
                    propertyDic =[[NSMutableDictionary alloc] init];
    
                    obj = [[NSClassFromString([modes objectAtIndex:i]) alloc] init];
    
                    [GDataParseXML propertyMutableArray:propertyNames obj:obj];
    
                    [GDataParseXML propertyDictionary:propertyDic obj:obj];
    
                }
    
                else if(baseObj == nil && isArray){
    
                    baseObj = [[NSClassFromString([modes objectAtIndex:0]) alloc] init];
    
                    [GDataParseXML propertyMutableArray:propertyBase obj:baseObj];
    
                    [GDataParseXML propertyDictionary:propertyDicBase obj:baseObj];
    
                    
    
                }
    
                for (GDataXMLNodeAnother * node in [(GDataXMLElementAnother *)thisObeject attributes]){
    
                    if (i==0) {
    
                        for (NSString* keystr in propertyBase) {
    
                            //修改,修改此处的判断 id 等于 或等于ID大写
    
                            if (([[node localName] isEqualToString:keystr]||[[node localName]isEqualToString:[keystr lowercaseString]]) && [[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [baseObj setValue:[node stringValue] forKey:keystr];
    
                                index++;
    
                            }
    
     
    
                        }
    
                        
    
                    }
    
                    else {
    
                        for (NSString* keystr in propertyNames) {
    
                            
    
                            if (([[node localName] isEqualToString:keystr]||[[node localName]isEqualToString:[keystr lowercaseString]]) && [[propertyDic objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [obj setValue:[node stringValue] forKey:keystr];
    
                                
    
                            }
    
                            
    
                        }
    
                    }
    
                    
    
                }
    
                for (GDataXMLElementAnother *node in [thisObeject children]){
    
                    NSString* temp = [[NSString alloc] initWithString:[node stringValue]];
    
                    if (i==0) {
    
                        for (NSString* keystr in propertyBase) {
    
                            //修改  ID id的情况
    
                            if (([[node localName] isEqualToString:keystr]||[[node localName]isEqualToString:[keystr lowercaseString]]) && [[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [baseObj setValue:temp forKey:keystr];
    
                                index++;
    
                            }
    
                        }
    
                        
    
                    }
    
                    else {
    
                        for (NSString* keystr in propertyNames) {
    
                            //修改
    
                            if (([[node localName] isEqualToString:keystr ] ||[[[node localName] lowercaseString]isEqualToString:[keystr lowercaseString]])&& [[propertyDic objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [obj setValue:temp forKey:keystr];
    
                                
    
                            }
    
                            
    
                        }
    
                    }
    
                    [temp release];
    
                }
    
                if (array != nil) {
    
                    [array addObject:obj];
    
                    [obj release];
    
                    obj = nil;
    
                    [propertyNames release];
    
                    [propertyDic release];
    
                }
    
                else if(array == nil && isArray){
    
                    [arrayForObjs addObject:baseObj];
    
                    [baseObj release];
    
                    baseObj = nil;
    
                    [propertyNames release];
    
                    [propertyDic release];
    
                }
    
                
    
            }
    
            if (array == nil) {
    
                [propertyNames release];
    
                [propertyDic release];
    
            }
    
            if (i!=0 && !isArray) {
    
                index++;
    
            }
    
            //处理新加节点用这个实体却没有新加节点的情况
    
            if (i==0) {
    
                for (NSString* keystr in propertyBase) {
    
                    if ([[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                        if([baseObj valueForKey:keystr]==nil){
    
                            index++;
    
                        }
    
                    }
    
                }
    
                
    
            }
    
            
    
            
    
            
    
            for (NSString* keystr in propertyBase) {
    
                if(((index-1) == [propertyBase indexOfObject:keystr]) && i!=0 && ![[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]){
    
                    if ([[propertyDicBase objectForKey:keystr] isEqualToString:@"NSMutableArray"]) {
    
                        [baseObj setValue:array forKey:keystr];
    
                        [array release];
    
                        array = nil;
    
                    }
    
                    else {
    
                        [baseObj setValue:obj forKey:keystr];
    
                    }
    
                }
    
                else if(((index+1) == [propertyBase indexOfObject:keystr]) && i!=0 && ![[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]){
    
                    [baseObj setValue:obj forKey:keystr];
    
                }
    
                
    
            }
    
            [obj release];
    
            obj = nil;
    
            
    
        }
    
        [propertyBase release];
    
        [propertyDicBase release];
    
        [doc release];
    
        if (isArray) {
    
            if (baseObj!= nil) {
    
                [baseObj release];
    
            }
    
            
    
            return [arrayForObjs autorelease];
    
        }
    
        else{
    
            return [baseObj autorelease];
    
        }
    
        
    
    }
    
    2、两层数组解析,对象里有数组对象
    
    基本同上,参考上面,将上层的model里面的属性改成执行类型model类的数组即可
    
    对象里面有数组,xml的节点肯定会比上面多一层,数组的那一层,所以参考这个nodes,就会多一层,model还是各自对应的moel
    +(id)parsePerformListModelWithXMLString:(NSString *)xmlString{
    
        NSMutableArray *nodes=[[NSMutableArray alloc]init];
    
        [nodes addObject:@"//root"];
    
        //数据模型内层数据节点
    
        [nodes addObject:@"//root//performance//performanceitem"];
    
        NSMutableArray *modes=[[NSMutableArray alloc]init];
    
        [modes addObject:@"PerformListModel"];
    
        [modes addObject:@"PerformanceItem"];
    
        id iret=[GDataParseXML utilitParseDataFromXML:xmlString nodeArrary:nodes modeArrary:modes andOpption:NO];
    
        //id iret = [GDataXMLUtil utilitParseDataFromXML:xmlString nodeArrary:nodes modeArrary:modes opption:NO];
    
        //GDataXMLUtil
    
        return iret;
    
    }
     

    3、三层数据解析:对象里套对象数组,套对象数组

    解析具体实现方法:

    + (id)utilitParseDataFromXML3:(NSString *)xmlString nodeArrary:(NSMutableArray*) nodes modeArrary:(NSMutableArray*) modes andOpption:(BOOL) isArray {
    
        NSError *error;
    
        GDataXMLDocumentAnother *doc = [[GDataXMLDocumentAnother alloc] initWithXMLString:xmlString options:0 error:&error];
    
        if (doc == nil) {
    
            return nil;
    
        }
    
        if ([nodes count] != [modes count]) {
    
            [doc release];
    
            return nil;
    
        }
    
        NSMutableArray * arrayForObjs = nil;
    
        if (isArray) {
    
            arrayForObjs = [[NSMutableArray alloc] init];
    
        }
    
        NSMutableArray *array = nil;
    
        NSMutableArray * propertyBase = [[NSMutableArray alloc] init];
    
        NSMutableDictionary *propertyDicBase =[[NSMutableDictionary alloc] init];
    
        
    
        id baseObj = [[NSClassFromString([modes objectAtIndex:0]) alloc] init];
    
        [GDataParseXML propertyMutableArray:propertyBase obj:baseObj];
    
        [GDataParseXML propertyDictionary:propertyDicBase obj:baseObj];
    
        
    
        int index =0;
    
        for (int i = 0;i< [nodes count]-1 ;i++) {
    
            id obj = nil;
    
            NSMutableArray * propertyNames =nil;
    
            NSMutableDictionary *propertyDic =nil;
    
            if (i!= 0) {
    
                for (NSString* keystr in propertyBase) {
    
                    if((index == [propertyBase indexOfObject:keystr]) && [[propertyDicBase objectForKey:keystr] isEqualToString:@"NSMutableArray"]){
    
                        array = [[NSMutableArray alloc] init];
    
                    }
    
                    
    
                }
    
                if (array == nil) {
    
                    propertyNames = [[NSMutableArray alloc] init];
    
                    propertyDic =[[NSMutableDictionary alloc] init];
    
                    obj = [[NSClassFromString([modes objectAtIndex:i]) alloc] init];
    
                    [GDataParseXML propertyMutableArray:propertyNames obj:obj];
    
                    [GDataParseXML propertyDictionary:propertyDic obj:obj];
    
                    
    
                }
    
            }
    
            NSArray *base = [doc nodesForXPath:[nodes objectAtIndex:i] error:nil];
    
            if (![base count]) {
    
                [array release];
    
                [obj release];
    
                obj = nil;
    
                array = nil;
    
            }
    
            
    
            for (GDataXMLNodeAnother *thisObeject in base){
    
                id iret = nil;
    
                if(i == 1){
    
                    NSMutableArray * nodes2 = [[NSMutableArray alloc] init];
    
                    [nodes2 addObject:[nodes objectAtIndex:2]];
    
                    
    
                    NSMutableArray * modes2 = [[NSMutableArray alloc] init];
    
                    [modes2 addObject:[modes objectAtIndex:2]];
    
                    
    
                    iret = [GDataParseXML utilitParseDataFromXML:thisObeject.XMLString nodeArrary:nodes2 modeArrary:modes2 andOpption:YES];
    
                    
    
                    [nodes2 release];
    
                    [modes2 release];
    
                }
    
                
    
                if (obj == nil && i!=0 && !isArray) {
    
                    propertyNames = [[NSMutableArray alloc] init];
    
                    propertyDic =[[NSMutableDictionary alloc] init];
    
                    obj = [[NSClassFromString([modes objectAtIndex:i]) alloc] init];
    
                    [GDataParseXML propertyMutableArray:propertyNames obj:obj];
    
                    [GDataParseXML propertyDictionary:propertyDic obj:obj];
    
                }
    
                else if(baseObj == nil && isArray){
    
                    baseObj = [[NSClassFromString([modes objectAtIndex:0]) alloc] init];
    
                    [GDataParseXML propertyMutableArray:propertyBase obj:baseObj];
    
                    [GDataParseXML propertyDictionary:propertyDicBase obj:baseObj];
    
                    
    
                }
    
                for (GDataXMLNodeAnother * node in [(GDataXMLElementAnother *)thisObeject attributes]){
    
                    if (i==0) {
    
                        for (NSString* keystr in propertyBase) {
    
                            if ([[node localName] isEqualToString:keystr] && [[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [baseObj setValue:[node stringValue] forKey:keystr];
    
                                index++;
    
                            }
    
                        }
    
                        
    
                    }
    
                    else {
    
                        for (NSString* keystr in propertyNames) {
    
                            if ([[node localName] isEqualToString:keystr] && [[propertyDic objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [obj setValue:[node stringValue] forKey:keystr];
    
                                
    
                            }
    
                            
    
                        }
    
                    }
    
                    
    
                }
    
                for (GDataXMLElementAnother *node in [thisObeject children]){
    
                    NSString* temp = [[NSString alloc] initWithString:[node stringValue]];
    
                    if (i==0) {
    
                        for (NSString* keystr in propertyBase) {
    
                            if ([[node localName] isEqualToString:keystr] && [[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [baseObj setValue:temp forKey:keystr];
    
                                index++;
    
                            }
    
                        }
    
                        
    
                    }
    
                    else {
    
                        for (NSString* keystr in propertyNames) {
    
                            if ([[node localName] isEqualToString:keystr] && [[propertyDic objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                                [obj setValue:temp forKey:keystr];
    
                                
    
                            }
    
                            
    
                            if ([[propertyDic objectForKey:keystr] isEqualToString:@"NSMutableArray"]) {
    
                                [obj setValue:iret forKey:keystr];
    
                                
    
                                
    
                                
    
                            }
    
     
    
                        }
    
                    }
    
                    [temp release];
    
                }
    
                if (array != nil) {
    
                    
    
                    [array addObject:obj];
    
                    [obj release];
    
                    obj = nil;
    
                    [propertyNames release];
    
                    [propertyDic release];
    
                }
    
                else if(array == nil && isArray){
    
                    [arrayForObjs addObject:baseObj];
    
                    [baseObj release];
    
                    baseObj = nil;
    
                    [propertyNames release];
    
                    [propertyDic release];
    
                }
    
                if (iret != nil) {
    
                    //[iret release];
    
                }
    
                
    
            }
    
            if (array == nil) {
    
                [propertyNames release];
    
                [propertyDic release];
    
            }
    
            if (i!=0 && !isArray) {
    
                index++;
    
            }
    
            //处理新加节点用这个实体却没有新加节点的情况
    
            if (i==0) {
    
                for (NSString* keystr in propertyBase) {
    
                    if ([[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]) {
    
                        if([baseObj valueForKey:keystr]==nil){
    
                            index++;
    
                        }
    
                    }
    
                }
    
                
    
            }
    
            
    
           
    
            for (NSString* keystr in propertyBase) {
    
                if(((index-1) == [propertyBase indexOfObject:keystr]) && i!=0 && ![[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]){
    
                    if ([[propertyDicBase objectForKey:keystr] isEqualToString:@"NSMutableArray"]) {
    
                        [baseObj setValue:array forKey:keystr];
    
                        [array release];
    
                        array = nil;
    
                    }
    
                    else {
    
                        [baseObj setValue:obj forKey:keystr];
    
                    }
    
                }
    
                else if(((index+1) == [propertyBase indexOfObject:keystr]) && i!=0 && ![[propertyDicBase objectForKey:keystr] isEqualToString:@"NSString"]){
    
                    [baseObj setValue:obj forKey:keystr];
    
                }
    
                
    
            }
    
            [obj release];
    
            obj = nil;
    
            
    
        }
    
        [propertyBase release];
    
        [propertyDicBase release];
    
        [doc release];
    
        if (isArray) {
    
            if (baseObj!= nil) {
    
                [baseObj release];
    
            }
    
            
    
            return [arrayForObjs autorelease];
    
        }
    
        else{
    
            return [baseObj autorelease];
    
        }
    
        
    
    }

    json解析:

    ESJsonFormat和Json model的配合使用自动生成model爽翻天

    生成model示例:

    @class ComplaintlistItem;
    
    @interface AppealListModel : NSObject
    
     
    
    @property (nonatomic, assign) NSInteger IsSuccess;
    
     
    
    @property (nonatomic, assign) NSInteger RecordCount;
    
     
    
    @property (nonatomic, copy) NSString *ErrorMessage;
    
     
    
    @property (nonatomic, strong) NSArray<ComplaintlistItem *> *ComplaintList;
    
     
    
    @end
    
    @interface ComplaintlistItem : NSObject
    
     
    
    @property (nonatomic, copy) NSString *OrderID;
    
     
    
    @property (nonatomic, copy) NSString *YzName;
    
     
    
    @property (nonatomic, copy) NSString *ComplaintStateName;
    
     
    
    @property (nonatomic, assign) NSInteger ComplaintID;
    
     
    
    @property (nonatomic, assign) NSInteger Frequency;
    
     
    
    @property (nonatomic, copy) NSString *Content;
    
     
    
    @property (nonatomic, assign) NSInteger ComplaintState;
    
     
    
    @property (nonatomic, copy) NSString *CreateTime;
    
     
    
    @end
    
     

    实现:

    @implementation AppealListModel
    
     
    
     
    
    + (NSDictionary *)objectClassInArray{
    
        return @{@"ComplaintList" : [ComplaintlistItem class]};
    
    //上层的属性,和其对应的model类的一一对应字典,如果多层嵌套,在相应的上层类实现文件中这样指明解析对应关系即可
    
    }
    
    @end
    
    @implementation ComplaintlistItem
    
     
    
    @end
  • 相关阅读:
    第四十七课、父子间的冲突
    第四十六课、继承中的构造与析构
    第四十五课、不同的继承方式
    第四十四课、继承中的访问级别
    第四十三课、继承的概念和意义
    第四十二课、类型转换函数(下)
    第四十一课、类型转化构造函数(上)
    暴力大法好
    HideProcess
    Hduacm—5497
  • 原文地址:https://www.cnblogs.com/Jordandan/p/6168819.html
Copyright © 2011-2022 走看看