#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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。