因为GDataXML的内部实现是通过DOM方式解析的,而在iOS 开发中用DOM方式解析xml文件,这个时候我们需要开启DOM,因为ios 开发中是不会自动开启的,只有在mac 开发中才自动开启的。我们需要做如下配置:
当配置玩这个操作之后,再次进行编译的时候,系统还是报错,是因为我们还需要进行如下操作:
// // ViewController.m // 04-xml解析GDataXML(DOM方式) // // Created by jerry on 15/9/29. // Copyright (c) 2015年 jerry. All rights reserved. // #import "ViewController.h" #import "GDataXMLNode.h" #import "Video.h" @interface ViewController () @property(nonatomic,strong)NSMutableArray *videos; @end @implementation ViewController -(NSMutableArray *)videos { if (_videos ==nil) { _videos = [NSMutableArray array]; } return _videos; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/videos.xml"]; // 2.send request NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3.connection [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 将xml 整个文件读入内存,对应的就是GDataXMLDocument 对象 GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data error:NULL]; // 对应的是 videos // NSLog(@"%@",document.rootElement.children); for (GDataXMLElement *element in document.rootElement.children) { // 对应的是video对象,所以就在这里创建video对象 // NSLog(@"%@",element); Video *video = [[Video alloc] init]; // 这个遍历 就是取出video对象对应的所有属性 for (GDataXMLElement *node in element.children) { // 拿到所有的值,对应的是每一个属性 // NSLog(@"%@----%@",node.name,node.stringValue); [video setValue:node.stringValue forKeyPath:node.name]; } // 跟踪属性 // NSLog(@"%@",element.attributes); // 遍历element.attributes 数组,拿到属性值 for (GDataXMLNode *att in element.attributes) { // kvc 进行赋值 [video setValue:att.stringValue forKeyPath:att.name]; } // 模型添加到一个数组 [self.videos addObject:video]; } NSLog(@"搞定。。。%@",self.videos); }]; } @end
video.h
#import <Foundation/Foundation.h> @interface Video : NSObject @property (nonatomic, copy) NSNumber *videoId; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSNumber *length; @property (nonatomic, copy) NSString *videoURL; @property (nonatomic, copy) NSString *imageURL; @property (nonatomic, copy) NSString *desc; @property (nonatomic, copy) NSString *teacher; @property (nonatomic, readonly) NSString *time; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)videoWithDict:(NSDictionary *)dict; @end
video.m
#import "Video.h" @implementation Video - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)videoWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } - (NSString *)time { int len = self.length.intValue; return [NSString stringWithFormat:@"%02d:%02d:%02d", len / 3600, (len % 3600) / 60, (len % 60)]; } - (NSString *)description { return [NSString stringWithFormat:@"<%@ : %p> { videoId : %@, name : %@, length : %@, videoURL : %@, imageURL : %@, desc : %@, teacher : %@}", [self class], self, self.videoId, self.name, self.length, self.videoURL, self.imageURL, self.desc, self.teacher]; } @end
如果还报错,引入log
#import <Foundation/Foundation.h> @interface NSArray (Log) @end @interface NSDictionary (Log) @end
#import "NSArray+Log.h" @implementation NSArray (Log) - (NSString *)descriptionWithLocale:(id)locale { NSMutableString *strM = [NSMutableString stringWithString:@"( "]; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [strM appendFormat:@" %@, ", obj]; }]; [strM appendString:@")"]; return strM; } @end @implementation NSDictionary (Log) - (NSString *)descriptionWithLocale:(id)locale { NSMutableString *strM = [NSMutableString stringWithString:@"{ "]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [strM appendFormat:@" %@ = %@; ", key, obj]; }]; [strM appendString:@"} "]; return strM; } @end