利用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文件)