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>