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

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

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

import "ViewController.h"

import "Student.h"

import "GDataXMLNode.h"

@interface ViewController ()
@property (nonatomic, retain) NSMutableArray *strArr;
@end

@implementation ViewController

  • (void)dealloc
    {
    [_strArr 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 *domB = [UIButton buttonWithType:UIButtonTypeSystem];
    domB.frame = CGRectMake(100, 100, 100, 30);
    [domB setTitle:@"XML解析/DOM" forState:UIControlStateNormal];
    [domB addTarget:self action:@selector(domAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:domB];
    }
  • (void)domAction
    {
    // 获取文件
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
    // 读取文件
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    // 创建数组
    self.strArr = [NSMutableArray array];
    GDataXMLDocument *xmlD = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    GDataXMLElement *xmlE = xmlD.rootElement;
    NSLog(@"整体信息:%@", xmlE);
    for (GDataXMLElement *subXmlE in xmlE.children) {

    NSLog(@"每一个学生的整体信息%@", subXmlE);
    // 注意学生对象的创建时机
    Student *stu = [[Student alloc] init];
    [self.strArr addObject:stu];
    [stu release];
    for (GDataXMLElement *element in subXmlE.children) {
        NSLog(@"每一个学生的每条信息:%@", element);
        NSLog(@"%@ %@", element.name, element.stringValue);
        // 为每一个学生设置信息
        [stu setValue:element.stringValue forKey:element.name];
        NSLog(@"%@", stu);
    }

    }
    [xmlD release];
    }

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

@end



//
// Student.h
// XML-DOM-解析
//
// 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-DOM-解析
//
// 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