码迷,mamicode.com
首页 > 其他好文 > 详细

利用GDataXML解析XML文件

时间:2016-02-01 22:23:32      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

1.导入GDataXMLNode.h 和 GDataXMLNode.m文件

2.导入libxml2库文件

3.工程target下Bulid Settings  搜索search 

找到Hearder Search Paths 添加路径/usr/include/libxml2

4.GDataXML不支持ARC 所以需要在Building Phases下的Complie Sources 对应的文件下 添加-fno-objc-arc

示例代码:

    
    NSString *path = [[NSBundle mainBundle] pathForResource:xmlName ofType:@"xml"];
    
    NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
    
    NSData *data = [file readDataToEndOfFile];
    
    GDataXMLDocument *doucument = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
    
    GDataXMLElement *rootElement = [doucument rootElement];
    
    NSArray *booksElement = [rootElement elementsForName:@"book"];
    NSMutableArray *books = [NSMutableArray array];
    
    for (GDataXMLElement *element in booksElement) {
        WJLBookModel *book = [[WJLBookModel alloc] init];
        book.bookID = [[[element attributeForName:@"id"] stringValue] integerValue];
        book.title = [[[element elementsForName:@"title"] lastObject] stringValue];
        book.author = [[[element elementsForName:@"author"] lastObject]stringValue];
        book.remark = [[[element elementsForName:@"remark"] lastObject] stringValue];
        [books addObject:book];
    }
    
    
    return [books copy]

 

利用GDataXML解析XML文件

标签:

原文地址:http://www.cnblogs.com/cnman/p/5176382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!