第一部分XML数据
<?xml version="1.0"?><authors><author name="DouglasAdams"><book title="The Hitchhiker's Guide to the Galaxy11111111" price="15.49"><description>1111111 Douglas Adams's hapless hero Arthur Dent as he travels the galaxy with his intrepid pal Ford Prefect, getting into horrible messes and generally wreaking hilarious havoc.</description></book><book title="The Restaurant at the End of the Universe222222 " price="14.36"><description>2222222Arthur and Ford, having survived the destruction of Earth by surreptitiously hitching a ride on a Vogon constructor ship, have been kicked off that ship by its commander. Now they find themselves aboard a stolen Improbability Drive ship commanded by Beeblebrox, ex-president of the Imperial Galactic Government and full-time thief.</description></book></author><author name="J.K. Rowling"><book title="Harry Potter and the Philosopher's Stone" price="119.99"><description>Harry potter thinks he is an ordinary boy - until he is rescued from a beetle-eyed giant of a man, enrolls at Hogwarts School of Witchcraft and Wizardry, learns to play quidditch and does battle in a deadly duel.</description></book><book title="Harry Potter and the Chamber of Secrets" price="8.99"><description>When the Chamber of Secrets is opened again at the Hogwarts School for Witchcraft and Wizardry, second-year student Harry Potter finds himself in danger from a dark power that has once more been released on the school.</description></book><book title="Harry Potter and the Prisoner of Azkaban" price="12.99"><description>Harry Potter, along with his friends, Ron and Hermione, is about to start his third year at Hogwarts School of Witchcraft and Wizardry. Harry can't wait to get back to school after the summer holidays. (Who wouldn't if they lived with the horrible Dursleys?) But when Harry gets to Hogwarts, the atmosphere is tense. There's an escaped mass murderer on the the loose, and the sinister prison guards of Azkaban have been called in to guard the school.</description></book></author>
</authors>

第二部分:数据模型(作者模型)

//
//  ZKZAuthor.h
//  使用TBXml解析
//
//  Created by 张凯泽 on 16/2/22.
//  Copyright © 2016年 rytong_zkz. All rights reserved.
//

#import <Foundation/Foundation.h>@interface ZKZAuthor : NSObject
@property(nonatomic,copy)NSString *authorName;//作者名字
@property(nonatomic,strong)NSMutableArray *books;//各种书籍@end

第三部分数据模型(书籍模型)

//
//  ZKZBook.h
//  使用TBXml解析
//
//  Created by 张凯泽 on 16/2/22.
//  Copyright © 2016年 rytong_zkz. All rights reserved.
//

#import <Foundation/Foundation.h>@interface ZKZBook : NSObject
@property(nonatomic,copy)NSString *price;//书的价格
@property(nonatomic,copy)NSString *title;//书的名字
@property(nonatomic,copy)NSString *descriptionStr;//书的表述@end

第四部分解析XML主体
//  ViewController.m
//  使用TBXml解析
//
//  Created by 张凯泽 on 15/11/24.
//  Copyright © 2015年 rytong_zkz. All rights reserved.
//

#import "ViewController.h"
#import "TBXML.h"
#import "ZKZAuthor.h"
#import "ZKZBook.h"
@interface ViewController ()
@property(nonatomic,strong)TBXML *tbXml;
//@property(nonatomic,strong)NSMutableArray *books;//书籍的数组
@property(nonatomic,strong)NSMutableArray *authors;//作者数组@end@implementation ViewController
@synthesize tbXml;
@synthesize authors;
- (void)viewDidLoad {[super viewDidLoad];NSError *error;tbXml = [[TBXML alloc]initWithXMLFile:@"books2.xml" error:&error];//[self startTBXml:tbXml];//[self dealUnknow:tbXml];//把XML数据转化成模型
    [self loadArticles:tbXml];}
-(void)startTBXml:(TBXML*)tbXml
{TBXMLElement *root = tbXml.rootXMLElement;TBXMLElement *author = [TBXML childElementNamed:@"author" parentElement:root];NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:author];NSLog(@"author:%@", name);TBXMLElement *book = [TBXML childElementNamed:@"book" parentElement:author];NSString *title = [TBXML valueOfAttributeNamed:@"title" forElement:book];NSLog(@"title:%@", title);NSString *price = [TBXML valueOfAttributeNamed:@"price" forElement:book];NSLog(@"price:%@", price);TBXMLElement *descriptionElem = [TBXML childElementNamed:@"description" parentElement:book];NSString * description = [TBXML textForElement:descriptionElem];NSLog(@"author:%@", description);
}- (void)dealUnknow:(TBXML *)tbx {if (tbx.rootXMLElement) {TBXMLElement *element = tbx.rootXMLElement;[self recurrence:element];}else {NSLog(@"Format Error!");}tbXml = nil;
}
//递归子方法
- (void)recurrence:(TBXMLElement *)element {do {NSLog(@"<%@>:{%@}",[TBXML elementName:element], [TBXML textForElement:element]);// Display the name of the element//迭代处理所有属性TBXMLAttribute * attribute = element->firstAttribute;while (attribute) {//显示NSLog(@"___****___<%@>->[%@ = %@]", [TBXML elementName:element], [TBXML attributeName:attribute], [TBXML attributeValue:attribute]);//迭代attribute = attribute->next;}//递归处理子树if (element->firstChild) {[self recurrence:element->firstChild];}//迭代处理兄弟树} while ((element = element->nextSibling));
}
- (void)loadArticles:(TBXML*)tbxml {TBXMLElement *root = tbxml.rootXMLElement;//articlesif (root){authors = [NSMutableArray array];//作者数组TBXMLElement *channel = [TBXML childElementNamed:@"author" parentElement:root];//作者while (channel) {NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:channel];//作者名字ZKZAuthor * author = [[ZKZAuthor alloc]init];//创建作者模型author.authorName = name;if (channel){NSMutableArray *books = [NSMutableArray array];//书籍数组TBXMLElement *item = [TBXML childElementNamed:@"book" parentElement:channel];//书籍while (item){ZKZBook * book = [[ZKZBook alloc]init];//创建书籍模型//书的名字NSString *title = [TBXML valueOfAttributeNamed:@"title" forElement:item];book.title = title;//书的价格NSString *price = [TBXML valueOfAttributeNamed:@"price" forElement:item];book.price = price;TBXMLElement *descriptionElem = [TBXML childElementNamed:@"description" parentElement:item];//书的描述NSString * description = [TBXML textForElement:descriptionElem];book.descriptionStr = description;[books addObject:book];//寻找下一个书籍item = [TBXML nextSiblingNamed:@"book" searchFromElement:item];}author.books = books;}[authors addObject:author];//寻找下一个作者channel = [TBXML nextSiblingNamed:@"author" searchFromElement:channel];}}for (ZKZAuthor * author  in authors) {NSLog(@"authorName = %@",author.authorName);for (ZKZBook * book in author.books) {NSLog(@"title = %@",book.title);NSLog(@"price = %@",book.price);NSLog(@"descriptionStr = %@",book.descriptionStr);}}tbxml = nil;
}
@end

转载于:https://www.cnblogs.com/zkzzkz/p/5209281.html

利用第三方库XML解析 (TBXML)转化成模型数据相关推荐

  1. python分析pcap文件_利用Python库Scapy解析pcap文件的方法

    每次写博客都是源于纳闷,python解析pcap这么常用的例子网上竟然没有,全是一堆命令行执行的python,能用吗?玩呢? pip安装scapy,然后解析pcap: import scapy fro ...

  2. Python dataframe指定列顺序输出 + 列数据转化成字符 + 数据框转化成列表

    参考: (3条消息)pandas中的DataFrame按指定顺序输出所有列 - quintind的专栏 - CSDN博客 https://blog.csdn.net/quintind/article/ ...

  3. EXCEL中把文本数据转化成数字数据

    在EXCEL中,用公式计算值得时候,经常会遇见无法计算,出现#VALUE!的情况. 如: 此时,单击报错的感叹号-----再点击"显示计算步骤" 我们发现,就是的数字1997,是由 ...

  4. 字符串不替代_使用第三方库demjson解析不规范的json字符串

    Json在编程中是一种轻量级的文件格式,在本地开发或者web开发中使用较多.JSON(JavaScript Object Notation,JavaScript对象表示法,读作/ˈdʒeɪsən/)是 ...

  5. 协程的概念及Python中利用第三方库gevent使用协程

    提到程序的并发操作,大多数人程序员首先想到的进程或者线程.我们先复习一下进程和线程的概念.   进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的 ...

  6. python requests post请求_实例解析Python3 如何利用requests 库进行post携带账号密码请求数据...

    1 调试过程 用Python3.6+Sciter+PyCharm写了一个py测试脚本helloworld.py,该脚本中只含有一条语句"import sciter".在PyChar ...

  7. 利用第三方库实现sftp上传文件

    封装一个类CSftp upload_sftp.h #define UPLOAD_SFTP_H #include <QString> #include <string> #inc ...

  8. vivo手机定位局限性,如何利用第三方库只获取定位信息

    众所周知,定位有多种方式,GPS,Network,Wifi,基站定位.其实最好的用户体验就是把这四种定位方式融合,除非手机没有卡(当然也不开wifi&&gps),不然总是能找到一个相对 ...

  9. python安装poi第三方库_使用Python获取城市POI数据

    1.数据接口: 本次使用百度地图开放平台中的地点检索API来获取城市POI数据,此次以矩形区域检索为例. 2.获取思路: 因为百度出于数据保护目的,单次访问服务最多同时返回400条数据,不过官方也给出 ...

  10. [转载] JAVA从菜鸟【入门】到新手【实习】一一一一Python 内置函数,标准库与第三方库(拓展库),常用框架

    参考链接: copyreg -注册pickle支持的函数 掌握了python的基本语法和面向对象的设计思想后是学习了程序设计的"使用"规则, 具体实现和调用要依赖"标准库 ...

最新文章

  1. PHP 常用字符串处理代码片段
  2. Android OpenGL使用GLSurfaceView预览视频
  3. Leader晋升失败,CTO说,没有商业敏感度,迟早被淘汰
  4. 【完结】给新手的12大深度学习开源框架快速入门项目
  5. 如何有效完成医学科研课题设计?
  6. 4 网络、挂载、关机
  7. c语言编程三问三答,c语言程序编程
  8. OpenCV中文路径问题、matplotlib可视化中文乱码问题的解决办法
  9. Django魔术用法
  10. LeetCode刷题(2)
  11. 华为腾讯众安微众360大咖齐聚,2019中国区块链开发者大会首批议程曝光!
  12. 4_蒙特卡罗算法求圆周率PI
  13. 使用微信企业号群发员工工资条
  14. XCTF练习题---MISC---simple_transfer
  15. linux服务器安装网卡驱动,Linux下如何安装网卡驱动
  16. 2019年厦门国际银行“数创金融杯”数据建模大赛总结
  17. 芴基噁二唑铱配合物|阳离子型铱配合物Ir(F_2ppy)_2(Br_2bpy)+PF-6
  18. [巩固培元]Python文件操作案例——用户登录
  19. rk3066 android4.4,Rooting the Cube U30GT rk3066 android tablet
  20. java第三方类库Guava开源组件使用

热门文章

  1. for循环与foreach的性能比较
  2. TP框架设置的LOG_LEVEL不起作用
  3. 【BZOJ2407/4398】探险/福慧双修 最短路建模
  4. 删除.svn 隐藏文件
  5. [Flex]浅析Mate flex framework在实际项目中的应用(二)
  6. 驱动调试(六)利用中断打印
  7. 阿里2015回顾面试招收学历(获得成功offer)
  8. Python使用TCPServer编写(多线程)Socket服务
  9. 修改ewebedit编辑器图片上传大小限制
  10. 将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)...