DOM解析

#import "GDataXMLNode.h"



{

NSMutableArray *arr;

UITableView *tab;

}



{    [super viewDidLoad];        NSURL *url=[NSURL URLWithString:MAC];    NSURLSession *ses=[NSURLSession sharedSession];    NSURLSessionDataTask *tax=[ses dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        //DOM解析        GDataXMLDocument * doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];        //跟标签内容 最外层标签所有内容        GDataXMLElement *root=[doc rootElement];        arr=[[NSMutableArray alloc]init];        //从跟标签中获得所有的hero标签        for (GDataXMLElement *heroEle  in [root elementsForName:@"hero"])        {            Model *mo=[[Model alloc]init];            //在标签获取所有内容标签

NSArray *naArr= [heroEle elementsForName:@"name"];

//获取唯一的一个标签

GDataXMLElement * nameEle=[naArr firstObject];

mo.name=[nameEle stringValue];

mo.li =[[[heroEle elementsForName:@"li" ]firstObject]stringValue];

//添加到数组

[arr addObject:mo];

}

}];

[tax resume];

tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

tab.delegate=self;

tab.dataSource=self;

[self.view addSubview:tab];

[tab reloadData];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{

return arr.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *str=@"cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];

if (cell==nil)

{

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];

}

Model *m=arr[indexPath.row];

cell.textLabel.text=m.name;

cell.detailTextLabel.text=m.li;

return cell;

}

#define MAC @"http://127.0.0.1/1506.xml"

你可能感兴趣的:(DOM解析)