当前位置: 首页 > 工具软件 > TBXML > 使用案例 >

TBXML库的用法

叶鸿振
2023-12-01
-(id)initWithMgzListXML:(NSString *)xmlString{
    self = [super init];
    if(self){
        self.contentArray = [[NSMutableDictionary alloc] init];
        NSString *xml = xmlString;
        xml =[xml stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
        xml =[xml stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
        xml =[xml stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
        xml =[xml stringByReplacingOccurrencesOfString:@">" withString:@">"];
        xml =[xml stringByReplacingOccurrencesOfString:@""" withString:@"\""];
        NSLog(@"xml is ------------>%@",xml);
        mainXMLSource = [[TBXML alloc] initWithXMLString:xml];
        TBXMLElement *root = mainXMLSource.rootXMLElement;
        if(root){
            TBXMLElement *contents = [TBXML childElementNamed:@"MagazineList" parentElement:root];
            if(contents){
                TBXMLElement *content = [TBXML childElementNamed:@"Magazine" parentElement:contents];
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];  
                while(content){
                    //解析属性
                    NSString *idStr = [NSString stringWithString:[TBXML valueOfAttributeNamed:@"Id" forElement:content]];
                    NSString *titleStr = [TBXML valueOfAttributeNamed:@"Title" forElement:content];
                    NSString *subTitleStr = [TBXML valueOfAttributeNamed:@"Subtitle" forElement:content];
                    NSString *categoryIdStr = [TBXML valueOfAttributeNamed:@"CategoryId" forElement:content];
                    NSString *categoryNameStr = [TBXML valueOfAttributeNamed:@"CategoryName" forElement:content];
                     [dict setObject:idStr forKey:@"Id"];
                     [dict setObject:titleStr forKey:@"Title"];
                     [dict setObject:subTitleStr forKey:@"Subtitle"];
                     [dict setObject:categoryIdStr forKey:@"CategoryId"];
                     [dict setObject:categoryNameStr forKey:@"CategoryName"];
                    //解析元素代码
                    TBXMLElement *description =  [TBXML childElementNamed:@"Description" parentElement:content];
                    if(description){
                        NSString *str = [TBXML textForElement:description];
                        [dict setObject:str forKey:@"Description"];
                    }
                    TBXMLElement *modifyDate = [TBXML childElementNamed:@"ModifyDate" parentElement:content];
                    if(modifyDate){
                        NSString *str = [TBXML textForElement:modifyDate];
                        [dict setObject:str forKey:@"ModifyDate"];
                    }
                    
                    NSMutableArray *list = [self.contentArray objectForKey:categoryIdStr];
                    if(list == nil || [list count]==0){
                        list = [NSMutableArray arrayWithCapacity:1];
                    }
                    [list addObject:dict];
                    [self.contentArray setObject:list forKey:categoryIdStr];
                    //给下一个元素赋值,配合WHILE循环使用
                    content = [TBXML nextSiblingNamed:@"Magazine" searchFromElement:contents];
                }
                //
            }
        }
        
    }

    return self;
}

 类似资料: