DOM解析

1.导入文件夹DOM解析类第三方包
2.创建model文件继承与NSObject
3.model.h中写入属性

@property (nonatomic,strong)NSString *name,*age,*sex;

4.APP.m中写入导航

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    return YES;
}

5.v.m中写入内容

#import "ViewController.h"
#import "GDataXMLNode.h"
#import "model.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *shujuyuan;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"DOM解析";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"dd" style:UIBarButtonItemStyleDone target:self action:@selector(dj)];
    [self.view addSubview:self.tab];
    
}
-(UITableView *)tab{
    if(!_tab){
        _tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        self.tab.delegate=self;
        self.tab.dataSource=self;
    }
    return _tab;
}
-(NSMutableArray *)shujuyuan{
    if(!_shujuyuan){
        _shujuyuan=[[NSMutableArray alloc]init];
    }
    return _shujuyuan;
}
-(void)dj{
    NSURL *url=[NSURL URLWithString:@"http://127.0.0.1/student.xml"];
    //创建data
    NSData *data=[[NSData alloc]initWithContentsOfURL:url];
    //创建解析器
    GDataXMLDocument *document=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
    //创建根节点
    GDataXMLElement *element=[document rootElement];
    //创建子节点
    NSArray *array=[element elementsForName:@"student"];
    for(GDataXMLElement *e in array){
        model *m=[[model alloc]init];
        m.name=[[[e elementsForName:@"name"]firstObject]stringValue];
        m.age=[[[e elementsForName:@"age"]firstObject]stringValue];
        m.sex=[[[e elementsForName:@"sex"]firstObject]stringValue];
        [self.shujuyuan addObject:m];
    }
    [self.tab reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shujuyuan.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"11"];
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"11"];
    cell.textLabel.text=[self.shujuyuan[indexPath.row]name];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@,%@",[self.shujuyuan[indexPath.row]age],[self.shujuyuan[indexPath.row]sex]];
    return cell;
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值