zoukankan      html  css  js  c++  java
  • 基本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>
     
  • 相关阅读:
    [BZOJ1584][Usaco2009 Mar]Cleaning Up 打扫卫生
    CSS浮动
    Django by example -----1总结
    C#函数重载
    linux目录的特点
    Linux调优
    linux
    对齐方式
    19-10-25-G-悲伤
    19-10-24-H
  • 原文地址:https://www.cnblogs.com/chillytao-suiyuan/p/4834173.html
Copyright © 2011-2022 走看看