前端之家收集整理的这篇文章主要介绍了
XMLParser (整理下以前写的代码),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#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
原文链接:https://www.f2er.com/xml/299485.html