zoukankan      html  css  js  c++  java
  • 利用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]
  • 相关阅读:
    leetcode 1. 两数之和
    leetcode 671. 二叉树中第二小的节点
    leetcode 100. 相同的树
    leetcode 110. 平衡二叉树
    leetcode 144. 二叉树的前序遍历
    1066. Root of AVL Tree (25)
    leetcode 100 相同的树
    leeCode 515 在每个树行中找最大值
    LeetCode 31.下一个排列
    面向对象UML中类关系
  • 原文地址:https://www.cnblogs.com/cnman/p/5176382.html
Copyright © 2011-2022 走看看