• 基本XML解析---编写


    #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>
     
  • 相关阅读:
    android内存知识普及抱怨墨迹内存大的同学看过来
    ICS 截屏(Screenshot)代码流程小结
    ICS Overlay主要流程
    Android中的sp和wp指针
    Android Display buffer_handle_t的定义
    adb shell input 命令
    百度地图API之MyLocationOverlay的使用(Android)
    由Android系统智能手机解锁图案引出的排列组合问题
    <script runat=server>、<%%>和<%#%>前台是服务器方法
    Oracle 日期运算 集合
  • 原文地址:https://www.cnblogs.com/chillytao-suiyuan/p/4834173.html
Copyright © 2020-2023  润新知