TOUCHXML解析xml
直奔主题。-》》》》》》》》》》》》》》
既然要使用touchXml解析xml。那么首先应该下载touchxml类库。猛击我。点击左边的zip下载。
打开下载好的类库。找到里面的Source文件夹。这就是需要用到的类库。
然后在xcode建立一个项目。在项目中新建一个文件夹,名字随便,就叫touchxml吧。然后将Source中的文件
拖入touchxml文件夹中。尽量将Copy itmes into destination group’s folder(if needed)勾选上。
这时候,你编译他是不会通过的。你需要首先添加libxml2 library。右键Frameworks。通过下面的操作
找到libxml2.dylib添加到工程中。这还没有结束,不能使用touchxml。还需要在工程选项中进行配置。
点击project-》Edit Porject Settings。在窗口的搜索栏中输入 header search.然后双击
Header Search Paths 后面空白。点击左下角+,打上对号,然后在Path中输入/usr/include/libxml2
在搜索框中输入other linker flags 。添加上字符-lxml2。然后command +B编译,success。
这个,我们就来个练习用一下这个touchxml。
起手我们用它的时候,先在项目里建立一个xml文件。没有xml解析什么呀。~~~~!
在Resources文件夹下Add-》New File,选择Mac OS x下的 Resource。然后选择stringFile。
文件名随意,就xmlfile.xml吧。内容可以复制下面的内容。
<books>
<iphonebook>
<id>01</id>
<name>iPhoneDeveloper's</name>
<page>123</page>
</iphonebook>
<iphonebook>
<id>02</id>
<name>ipadDeveloper's</name>
<page>220</page>
</iphonebook>
<wp7book>
<id>03</id>
<name>wp7developer</name>
<page>281 </page>
</wp7book>
<wp7book>
<id>04</id>
<name>windows phone 7</name>
<page>300</page>
</wp7book>
</books>
直接上代码说话吧。。。。记得引用它的头文件。#import "TouchXML.h"
- (void)viewDidLoad
{
//获得文件路径
NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"userInfo.xml"];
//取得数据
NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath];
//生成CXMLDocument对象
CXMLDocument *document = [[CXMLDocument alloc] initWithData:XMLData
options:0
error:nil
];
//[self parseDire:document];
[self parseRoot:document];
[super viewDidLoad];
}
[self parseDire:document];
[self parseRoot:document]; 这两个是两种解析的方式,可以分别尝试下。
- (void) parseDire:(CXMLDocument *) document
{
NSArray *books = NULL;
books = [document nodesForXPath:@"//iphonebook" error:nil];
for (CXMLElement *element in books)
{
if ([element isKindOfClass:[CXMLElement class]])
{
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [element childCount]; i++)
{
if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]])
{
[item setObject:[[element childAtIndex:i] stringValue]
forKey:[[element childAtIndex:i] name]
];
NSLog(@"%@", [[element childAtIndex:i] stringValue]);
}
}
//NSLog(@"%@", item);
}
}
//
books = [document nodesForXPath:@"//wp7book" error:nil];
for (CXMLElement *element in books)
{
if ([element isKindOfClass:[CXMLElement class]])
{
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [element childCount]; i++)
{
if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]])
{
[item setObject:[[element childAtIndex:i] stringValue]
forKey:[[element childAtIndex:i] name]
];
NSLog(@"%@", [[element childAtIndex:i] stringValue]);
}
}
//NSLog(@"%@", item);
}
}
}
-------------------------------
- (void) parseRoot:(CXMLDocument *) document
{
CXMLElement *root = [document rootElement];
NSArray *books = [root children];
for (CXMLElement *element in books)
{
if ([element isKindOfClass:[CXMLElement class]])
{
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [element childCount]; i++)
{
if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]])
{
if ([[element name] isEqualToString:@"iphonebook"])
{
[item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name]];
NSLog(@"1%@", [[element childAtIndex:i] stringValue] );
}
if ([[element name] isEqualToString:@"wp7book"])
{
[item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name]];
NSLog(@"2%@", [[element childAtIndex:i] stringValue] );
}
}
}
//NSLog(@"%@", item);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2008-11-18 JavaScript基础-3