• iOS UI 14 网络解析


    //

    //  RootViewController.m

    //  ui  - 14 数据解析

    //

    //  Created by dllo on 15/11/26.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "RootViewController.h"

    #import "Student.h"

    #import "GDataXMLNode.h"

    @interface RootViewController ()<NSXMLParserDelegate>

    @property (nonatomic, retain)NSMutableArray *stuArr;

    @property (nonatomic, retain)NSMutableArray *stuArr2;

    @property (nonatomic ,copy)NSString *elementStr;

    @end


    @implementation RootViewController

    - (void)dealloc

    {

        [_stuArr release];

        [_stuArr2 release];

    //    [_elementStr release];

        [super dealloc];

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

        button1.frame = CGRectMake(50, 100, 100, 50);

        button1.backgroundColor = [UIColor cyanColor];

        [button1 setTitle:@"XML/SAX" forState:UIControlStateNormal];

        [button1 addTarget:self action:@selector(saxAction) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button1];

        

        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

        button2.frame = CGRectMake(50, 200, 100, 50);

        button2.backgroundColor = [UIColor cyanColor];

        [button2 setTitle:@"XML/Dom" forState:UIControlStateNormal];

        [button2 addTarget:self action:@selector(domAction) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button2];

        

        

        

        

        

        

        // Do any additional setup after loading the view.

    }

    //dom sax 比较

    // dom 先将数据全部取到内存,在解析, sax逐行取数据并解析

    //dom更耗费内存,不适合大数据解析,但更加灵活, 可以解析获取指定行数据

    //sax不耗费内存,但不灵活,只能逐行按顺序解析


    /*******************************XMLsax工具解析************************/

    - (void)saxAction

    {

        NSString *file = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];

        //二进制流 安全

        NSData *data = [NSData dataWithContentsOfFile:file ];

        NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:data] autorelease];

        parser.delegate = self;

        //开始解析

        [parser parse];

        

        

    }

    - (void)parserDidStartDocument:(NSXMLParser *)parser

    {

        NSLog(@"开始解析");

        //开辟数组空间

        self.stuArr = [NSMutableArray array];

        

    }

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

    {

        NSLog(@"只解析开始标签:%@",elementName);

        

        //可以全局访问开始标签

        self.elementStr = elementName;

        

        //若开始标签是student,创建学生对象并添加到数组中

        if ([elementName isEqualToString:@"student"]) {

            Student *stu = [[Student alloc]init];

            [self.stuArr addObject:stu];

            [stu release];

        }

        

        

    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

    {

        NSLog(@"解析标签值:%@",string);

        //获取正在编辑的学生对象

        Student *stu = self.stuArr.lastObject;

        //为学生对象赋值

        if ([self.elementStr isEqualToString:@"name"]) {

            stu.name = string;

        }

        if ([self.elementStr isEqualToString:@"number"]) {

            stu.number = string;

        }

        if ([self.elementStr isEqualToString:@"sex"]) {

            stu.sex = string;

        }

        if ([self.elementStr isEqualToString:@"phone"]) {

            stu.phone = string;

        }


    }

    - (void)parserDidEndDocument:(NSXMLParser *)parser

    {

         NSLog(@"结束解析");

        

        for (Student *stu in self.stuArr) {

            NSLog(@"%@ %@ %@ %@",stu.name, stu.sex, stu.number, stu.phone);

        }

    }


    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

    {

        NSLog(@"只解析结束标签:%@",elementName);

        

        

        //********************************防止已经解析好的标签值被非预期的标签值覆盖

        self.elementStr =nil;

    }

    /*******************************XMLdom工具解析************************/

    -(void)domAction

    {

        self.stuArr2 = [NSMutableArray array];

        NSString *file = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];

        //二进制流 安全

        NSData *data = [NSData dataWithContentsOfFile:file ];

    //*****************添加第三方工具dom的方法

        //添加路径 : buliding setting - > Header Search paths - > 添加路径: /usr/include/libxml2

        // 添加库:building Phases -> Link... 添加库:libxml2.dyliblibxml2.tbd

        

        

        //参数1 从文件中读取的data对象

        //参数2 预留参数 0;

        //参数3 错误信息, 若不需要可写nil

        GDataXMLDocument *xmLD = [[[GDataXMLDocument alloc]initWithData:data options:0 error:nil]autorelease];

        GDataXMLElement *xmLE = xmLD.rootElement;

    //    NSLog(@"整体信息:%@", xmLE);

        for (GDataXMLElement *subxmle in xmLE.children) {

    //        NSLog(@"每名学生的信息:%@", subxmle);

            

            

            //创建学生对象

            

            Student *stu = [[Student alloc]init];

            

            for (GDataXMLElement *element in subxmle.children) {

    //            NSLog(@"具体信息,每一条信息, 如名字,学号,性别,电话:%@",element);

                //有一个属性

                //NSLog(@"标签:%@ 标签值:%@",element.name, element.stringValue);

                

                [stu setValue:element.stringValue forKey:element.name];

            }

            [self.stuArr2 addObject:stu];

            [stu release];

        }

        

        

        for (Student *stu in self.stuArr2) {

            NSLog(@"%@ %@ %@ %@",stu.name, stu.sex, stu.number, stu.phone);

        }

        

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }


    /*

    #pragma mark - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */


    @end


  • 相关阅读:
    对抗杀软高级启发(主动防御)技术
    __readfsdword __readgsqword PEB TEB
    net1 user
    vc 获取windows 网卡信息
    RunPE
    windows crpyt API 实现DES 3DES 3DES_112 加解密
    软件开发图标收藏
    有关windows 网卡信息获取
    BCGPContorBar 静态工程设置详解
    (二)特征匹配
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043077.html
Copyright © 2020-2023  润新知