iOS-UI-XML数据解析-SAX方法

iOS-UI-XML数据解析-SAX方法

iOS-UI-XML数据解析-SAX方法iOS-UI-XML数据解析-SAX方法
iOS-UI-XML数据解析-SAX方法

iOS-UI-XML数据解析-SAX方法
iOS-UI-XML数据解析-SAX方法
iOS-UI-XML数据解析-SAX方法
//
// ViewController.m
// XML解析
//
// Created by YIem on 16/2/29.
// Copyright © 2016年 YIem. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@property (nonatomic, retain) NSMutableArray *stuArr;
@property (nonatomic, copy) NSString *elementNameStr;
@end

@implementation ViewController

  • (void)dealloc
    {
    [_stuArr release];
    [super dealloc];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *saxB = [UIButton buttonWithType:UIButtonTypeSystem];
    saxB.frame = CGRectMake(100, 100, 80, 30);
    [saxB setTitle:@"XML解析" forState:UIControlStateNormal];
    [saxB addTarget:self action:@selector(saxAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saxB];
    }

  • (void)saxAction
    {
    // 获取文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
    // 读取文件内容
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    // XML解析
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    // 协议
    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<NSString *,NSString *> *)attributeDict
    {
    NSLog(@"只解析开始标签:%@", elementName);
    // 为了在"解析值"的方法内获取到当前的意义(为解析标签值服务)
    self.elementNameStr = 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;

    [stu setValue:string forKey:self.elementNameStr];
    }

  • (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
    NSLog(@"只解析结束标签:%@", elementName);
    // 数据解析为空(\n)的第二种解决办法
    self.elementNameStr = nil;
    }

  • (void)parserDidEndDocument:(NSXMLParser *)parser
    {
    NSLog(@"结束解析");
    for (Student *stu in self.stuArr) {
    NSLog(@"%@ %@ %@ %@", stu.number, stu.name, stu.sex, stu.phone);
    }
    }

  • (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
    {
    NSLog(@"不可恢复错误");
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end



//
// Student.h
// XML解析
//
// Created by YIem on 16/2/29.
// Copyright © 2016年 YIem. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, copy) NSString *number;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *phone;
@end


//
// Student.m
// XML解析
//
// Created by YIem on 16/2/29.
// Copyright © 2016年 YIem. All rights reserved.
//

#import "Student.h"

@implementation Student

  • (void)dealloc
    {
    [_number release];
    [_name release];
    [_sex release];
    [_phone release];
    [super dealloc];
    }
  • (void)setValue:(id)value forUndefinedKey:(NSString *)key
    {

}
@end

标签: none

添加新评论

Title - Artist
0:00