基本XML解析---编写
#import "DDXML.h"
@interface ViewController ()
@end
@implementation ViewController
/*
解析XML:开源框架:KissXML 依赖:libxml2.2.dylib(项目配置-->General-->Linked Frameworks and Libraries-->点击+号-->输入libxml2.2.dylib-->add)
如果使用libxml2.2.dylib,则需要在header search path中添加路经:(Build Settings --> Search Paths --> Header Search Paths中添加/usr/include/libxml2-->回车)
*/
- (void)viewDidLoad {
[super viewDidLoad];
//1.组装XML
[self builXML];
//2.解析XML
//解析XML的方式:
//DOM: 当前标签量少的时候
//SAX: 当前标签量大的时候
[self parseXML];
}
/**
<movie country="USA" > // 主标签:属性:country:USA
<title>X战警</title> //子标签title: X战警
<actor>Bob Dylan</actor> //子标签 actor: Bob Dylan
</movie>
*/
//组装XML
- (void)builXML {
//1.创建标签
DDXMLElement *element = [DDXMLElement elementWithName:@"movie"];
//设置属性
DDXMLDocument *document = [DDXMLDocument attributeWithName:@"country" stringValue:@"USA"];
[element addAttribute:document];
//2.添加子标签
DDXMLDocument *title = [DDXMLDocument elementWithName:@"title" stringValue:@"X战警"];
DDXMLDocument *actor = [DDXMLDocument elementWithName:@"actor" stringValue:@"Bob Dylan"];
[element addChild:title];
[element addChild:actor];
// NSLog(@"element:%@",element);
}
- (void)parseXML {
//获取
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"file.xml" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(@"%@",doc);
//xml文件中所有的价格
// NSString *xpath = @"/catalog//price";
//同上
// NSString *xpath = @"/catalog/*/price";
// cd下的所有内容
NSString *xpath = @"//*";
NSArray *arr = [doc nodesForXPath:xpath error:nil];
NSLog(@"--%@",arr);
for (DDXMLElement *element in arr) {
NSLog(@"%@",element.stringValue);
<catalog>
<cd1 country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd1>
<cd2 country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd2>
<cd3 country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd3>
最新文章
- FMDB基本使用
- shell实现https登录
- JavaWeb用Jdbc操作MySql数据库(一)
- node socket.io web
- Cheatsheet: 2015 01.01~ 01.31
- HDU 4662 MU Puzzle 简单找规律
- SQLite数据库简介(转)
- windows下rundll32介绍
- Google Map API 代码示例
- 4.Linux的文件搜索命令
- 81. Search in Rotated Sorted Array II (中等)
- Vue 环境搭建(win10)
- MySQL数据库快速造大量数据
- 记录 制作校园网登陆脚本 python编写 附源码
- IIC稳定性.VBS
- Codeforces 1079 E - The Unbearable Lightness of Weights
- NPC问题及证明
- shell常见命令
- 20145335郝昊《网络攻防》Exp4 MS11_050
- some ideas