#import "ViewController.h"
#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];
   
//解析
    DDXMLDocument *doc = [[DDXMLDocument alloc]initWithData:data options:0 error:nil];
    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);

    }
   
   
}
@end
 
 ----------------------------------------分割线-------------------------------------------------
//XML文档
 
<?xml version="1.0" encoding="UTF-8"?>
<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>
</catalog>
 

最新文章

  1. FMDB基本使用
  2. shell实现https登录
  3. JavaWeb用Jdbc操作MySql数据库(一)
  4. node socket.io web
  5. Cheatsheet: 2015 01.01~ 01.31
  6. HDU 4662 MU Puzzle 简单找规律
  7. SQLite数据库简介(转)
  8. windows下rundll32介绍
  9. Google Map API 代码示例
  10. 4.Linux的文件搜索命令
  11. 81. Search in Rotated Sorted Array II (中等)
  12. Vue 环境搭建(win10)
  13. MySQL数据库快速造大量数据
  14. 记录 制作校园网登陆脚本 python编写 附源码
  15. IIC稳定性.VBS
  16. Codeforces 1079 E - The Unbearable Lightness of Weights
  17. NPC问题及证明
  18. shell常见命令
  19. 20145335郝昊《网络攻防》Exp4 MS11_050
  20. some ideas

热门文章

  1. js 日期对象Date以及传参
  2. git分支管理一
  3. 如何将数据库中的表导入到PowerDesigner中
  4. PHP获取页面执行时间的方法
  5. html理解
  6. js原生ajax请求get post笔记
  7. canvas beginPath()
  8. [php-src]窥探Php内核中的数组与面向对象
  9. Some About Spring
  10. LeetCode(97) Interleaving String