ios xml解析

ios xml解析:

#import "XmlParse.h"
#import "TBXML.h"
#import "City.h"

@implementation XmlParse

+(NSMutableArray *)parseMenusByPath:(NSString *)path{
    NSMutableArray *citys = [NSMutableArray array];
    NSData *data = [NSData dataWithContentsOfFile:path];
    TBXML *tbxml = [[TBXML alloc]initWithXMLData:data error:nil];
    
    TBXMLElement *dataEle = tbxml.rootXMLElement;
    TBXMLElement *rowEle = [TBXML childElementNamed:@"row" parentElement:dataEle];
    
    //遍历所有的row元素
    while (rowEle) {
        City *city = [[City alloc]init];
        //得到元素属性
        TBXMLElement *DEPT_LEV_Ele  = [TBXML childElementNamed:@"LEV"           parentElement:rowEle];
        TBXMLElement *DEPT_ID_Ele   = [TBXML childElementNamed:@"DEPT_ID"       parentElement:rowEle];
        TBXMLElement *DEPT_CODE_Ele = [TBXML childElementNamed:@"DEPT_CODE"     parentElement:rowEle];
        TBXMLElement *DEPT_NAME_Ele = [TBXML childElementNamed:@"DEPT_NAME"     parentElement:rowEle];
        TBXMLElement *UPPER_DEPT_ID = [TBXML childElementNamed:@"UPPER_DEPT_ID" parentElement:rowEle];
        
        //得到元素间的文本
        city.LEV           = [TBXML textForElement:DEPT_LEV_Ele ];
        city.DEPT_ID       = [TBXML textForElement:DEPT_ID_Ele  ];
        city.DEPT_CODE     = [TBXML textForElement:DEPT_CODE_Ele];
        city.DEPT_NAME     = [TBXML textForElement:DEPT_NAME_Ele];
        city.UPPER_DEPT_ID = [TBXML textForElement:UPPER_DEPT_ID];
        
        [citys addObject:city];
        rowEle = [TBXML nextSiblingNamed:@"row" searchFromElement:rowEle];
    }
    return citys;
}

@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值