利用GDataXML解析XML文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用GDataXML解析XML文件相关的知识,希望对你有一定的参考价值。
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文件的主要内容,如果未能解决你的问题,请参考以下文章