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
上一篇: 春季多喝三款养生汤 消除疲惫保你精神抖擞
下一篇: vue输入框添加\删除标签