zoukankan      html  css  js  c++  java
  • iOS 学习

    准备工作:

    new -> file -> other -> Empty ,在 Save As: 中随便起个名字后缀为 .xml

    拷贝下面

    <person>
        <student>
                <pid>1001</pid>
                <name>小明</name>
                <sex></sex>
                <age>12</age>
        </student>
    </person>

    完整代码

    //
    //  ViewController.m
    //  XML
    //
    //  Created by CC on 16/8/29.
    //  Copyright © 2016年 CC. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<NSXMLParserDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self analysis];
    }
    
    - (void)analysis {
        
        NSString *filename = [[NSBundle mainBundle]pathForResource:@"File.xml" ofType:nil];
        NSData *data = [[NSData alloc]initWithContentsOfFile:filename];
        NSXMLParser *parset = [[NSXMLParser alloc]initWithData:data];
        parset.delegate = self;
        [parset parse];
    }
    
    //文档开始时触发,只触发一次
    - (void)parserDidStartDocument:(NSXMLParser *)parser {
        NSLog(@"parsetStart");
    }
    
    //文档出错时触发
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
        if (parseError) {
            NSLog(@"parseError:%@",parseError);
        }
    }
    
    //遇到字符串开始触发
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
        NSLog(@"00000000----- %@",string);
    }
    
    //遇到开始标签触发
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
        NSLog(@"arrributeDict --- %@",attributeDict);
    }
    
    //遇到结束标签触发
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
        NSLog(@"didEndElement ----- %@",elementName);
    }
    
    //遇到文档结束时触发
    - (void)parserDidEndDocument:(NSXMLParser *)parser {
        NSLog(@"DidEndDocument");
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

     

  • 相关阅读:
    Oracle-函数-split 和 splitstr 的创建
    git merge方法
    查看Android 设备进程id
    内存泄漏
    Mac显示隐藏文件快捷键
    gradle版本
    commit单一文件
    21不下发信号
    FileInputStream read函数何时返回-1
    maven turbonet目录
  • 原文地址:https://www.cnblogs.com/asamu/p/5820230.html
Copyright © 2011-2022 走看看