欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DOM解析

程序员文章站 2022-06-20 10:57:01
...

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];
    [email protected]"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

相关标签: DOM解析