GDataXML 解析XML

GDataXML 解析XML


#import "ViewController.h"
#import "GDataXMLNode.h"
#import "UserInfo.h"
#import "UserDetailInfo.h"

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *userInfoArray;
@property (nonatomic, strong) NSMutableArray *userDetailInfoArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUserInfoArray];
    [self createUserDetailInfoArray];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"userxml" ofType:@"xml"];
    NSData *xmlData = [[NSData alloc] initWithContentsOfFile:path];
    
    // 1. 加载档
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData error:nil];
    
    // 2. 获得根元素
    GDataXMLElement *element = doc.rootElement;
    
    // 3. 获得所有的user的根节点
    NSArray *elementArray = [element elementsForName:@"user"];
    
    for (GDataXMLElement *ele in elementArray) {
        
        // 1. 获取根节点的属性值,用模型接收属性
        UserInfo *userInfo = [[UserInfo alloc] init];
        userInfo.Id = [[ele attributeForName:@"Id"] stringValue];
        userInfo.token = [[ele attributeForName:@"token"] stringValue];
        userInfo.phone = [[ele attributeForName:@"phone"] stringValue];
        [self.userInfoArray addObject:userInfo];
        
        NSLog(@"%@,%@,%@",userInfo.Id,userInfo.token,userInfo.phone);
        
        // 2. 创建模型获取其中的值
        UserDetailInfo *userDetailInfo = [[UserDetailInfo alloc] init];
        
        // 2.1 获取子节点每个元素的值
        GDataXMLElement * usernameElement = [[ele elementsForName:@"username"] lastObject];
        userDetailInfo.username = [usernameElement stringValue];
        
        GDataXMLElement * sexElement = [[ele elementsForName:@"sex"] lastObject];
        userDetailInfo.sex = [sexElement stringValue];
        
        GDataXMLElement * photourlElement = [[ele elementsForName:@"photourl"] lastObject];
        userDetailInfo.photourl = [photourlElement stringValue];
        
        [self.userDetailInfoArray addObject:userDetailInfo];
        
        NSLog(@"%@,%@,%@",userDetailInfo.username,userDetailInfo.sex,userDetailInfo.photourl);
    }
}

- (NSMutableArray *)createUserInfoArray
{
    if (!_userInfoArray) {
        self.userInfoArray = [NSMutableArray new];
    }
    return self.userInfoArray;
}

- (NSMutableArray *)createUserDetailInfoArray
{
    if (!_userDetailInfoArray) {
        self.userDetailInfoArray = [NSMutableArray new];
    }
    return self.userDetailInfoArray;
}

@end





-----------------
UserDetailInfo 文件



@interface UserDetailInfo : NSObject

@property (nonatomic, copy) NSString *username;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *photourl;

@end



———————------------
UserInfo  文件

@interface UserInfo : NSObject

@property (nonatomic, copy) NSString *Id;
@property (nonatomic, copy) NSString *token;
@property (nonatomic, copy) NSString *phone;

- (id)initWithDict:(NSDictionary *)dict;
+ (id)userWitnDict:(NSDictionary *)dict;

@end

------------------------------

#import "UserInfo.h"

@implementation UserInfo

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}


+ (instancetype)userWitnDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

@end
-------------------------------

XML文件

<?xml version = "1.0" encoding = "UTF-8"?>
<users>
    <user Id="1" token="101" phone = "18801234561" >
        <username>张三</username>
        <sex>M</sex>
        <photourl>http://1.com</photourl>
    </user>
    
    <user Id="2" token="102" phone = "18801234562" >
        <username>李四</username>
        <photourl>http://2.com</photourl>
        <sex>F</sex>
    </user>
    <user Id="3" token="103" phone = "18801234563" >
        <username>王五</username>
        <photourl>http://3.com</photourl>
        <sex>M</sex>
    </user>
    <user Id="4" token="104" phone = "18801234564" >
        <username>赵六</username>
        <photourl>http://4.com</photourl>
        <sex>F</sex>
    </user>
</users>





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值