XMLParser (整理下以前写的代码)
发布时间:2020-12-16 05:12:34 所属栏目:百科 来源:网络整理
导读:#import "XMLViewController.h"@implementation XMLViewController//@synthesize xmlTextView;@synthesize myTableView;@synthesize xmlData,connection,xmlDataArray;-(void)viewDidLoad{ xmlData=[[NSMutableData alloc]init]; CGRect rect=CGRectMake(0,8
#import "XMLViewController.h" @implementation XMLViewController //@synthesize xmlTextView; @synthesize myTableView; @synthesize xmlData,connection,xmlDataArray; -(void)viewDidLoad { xmlData=[[NSMutableData alloc]init]; CGRect rect=CGRectMake(0,80,320,400); myTableView=[[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain]; self.myTableView.delegate=self; self.myTableView.dataSource=self; [self.view addSubview:myTableView]; } - (void)viewDidUnload { [super viewDidUnload]; } - (IBAction)startParseAction:(id)sender { NSURL *url=[NSURL URLWithString:@"http://192.168.0.110:8080/fw/index2.htm"]; NSURLRequest *req=[NSURLRequest requestWithURL:url]; connection=[[NSURLConnection alloc]initWithRequest:req delegate:self startImmediately:YES]; NSLog(@"begin fetch data"); } //接受数据 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [xmlData appendData:data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *xmlCheck=[[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding]; xmlDataArray=[[NSMutableArray alloc]initWithCapacity:0]; NSXMLParser *parser=[[NSXMLParser alloc]initWithData:xmlData]; [parser setShouldProcessNamespaces:NO]; [parser setShouldReportNamespacePrefixes:NO]; [parser setShouldResolveExternalEntities:NO]; [parser setDelegate: self]; [parser parse]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"student"]) { NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithCapacity:0]; [dic setObject:[attributeDict objectForKey:@"name"] forKey:@"studentName"]; [dic setObject:[attributeDict objectForKey:@"age"] forKey:@"studentAge"]; [dic setObject:[attributeDict objectForKey:@"sex"] forKey:@"studentSex"]; [xmlDataArray addObject:dic]; dic=nil; } } // -(void)parserDidEndDocument:(NSXMLParser *)parser { // NSString *temp=[NSString stringWithFormat:@"%@",xmlDataArray]; // [xmlTextView setText:temp]; NSLog(@"success"); [myTableView reloadData]; } /*======================设置tableView的行数=============================*/ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [xmlDataArray count]; } /*======================设置tableView每个cell的内容=============================*/ -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithCapacity:0]; dic=[xmlDataArray objectAtIndex:indexPath.row]; NSString *name=[NSString stringWithString:[dic objectForKey:@"studentName"]]; NSString *age=[NSString stringWithString:[dic objectForKey:@"studentAge"]]; NSString *sex=[NSString stringWithString:[dic objectForKey:@"studentSex"]]; NSString *temp=[NSString stringWithFormat:@"name:%@ age:%@ sex:%@",name,age,sex]; cell.textLabel.text=temp; return cell; } @end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |