ios 简单的rss 解析应用demo

1.主文件: RootViewController.h

#import <UIKit/UIKit.h>


@interface RootViewController : UITableViewController {
	
	NSMutableArray *rssList;
	NSMutableDictionary *currentItem;
	NSMutableString *currentContents;
	
    
}

@property (nonatomic, retain) NSMutableArray *rssList;



@end




#import "RootViewController.h"
#import "InternetNewsAppDelegate.h"
#import "DetailViewController.h"



@implementation RootViewController
@synthesize rssList;

#pragma mark -
#pragma mark View lifecycle



- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	self.title = @"国际要闻";
	
	self.rssList = [[NSMutableArray alloc] init];
	
	//NSXMLParser *firstParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://rss.sina.com.cn/news/world/focus15.xml"]];
    NSXMLParser *firstParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.alibuybuy.com/feed"]];
	firstParser.delegate = self;
   
	[firstParser parse];
	
}


#pragma mark -
#pragma mark NSXMLParser delegate Methods

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	NSLog(@"startParser----------------------");	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	NSLog(@"end:----------------------");
	[parser release];
	NSLog(@"%@",rssList);	
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if([elementName isEqualToString:@"item"])
	{
		currentItem = [[NSMutableDictionary alloc] init];
	}
	else if (currentItem != NULL)
	{
		currentContents = [[NSMutableString alloc] init];
	}
	
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if([elementName isEqualToString:@"item"])
	{
		[rssList addObject:currentItem];
		[currentItem release];
		
	}
	else if (currentContents && currentItem)
	{
		[currentItem setObject:currentContents forKey:elementName];
		[currentContents release];
		currentContents = nil;
		
	}
	
	
}



- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
	if(currentItem && currentContents)
	{
		currentContents = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if(currentItem && currentContents)
	{
		[currentContents appendString:string];		
	}	
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [rssList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	
	cell.textLabel.text = [[rssList objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate
//当点击cell后利用 NSNotificationCenter将消息传递给 DetailViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	
    NSString *des = [[rssList objectAtIndex:indexPath.row]objectForKey:@"description"];
    
    DetailViewController *detailView = [[DetailViewController alloc] init];
    NSLog(@"%@", self.navigationController.view);
    [[self navigationController] pushViewController:detailView animated:YES];
   
   
    [[NSNotificationCenter defaultCenter] postNotificationName:@"onClicked" object:des];
    
    [detailView release];

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[rssList release];
    [super dealloc];
}


@end

DetailViewController (详细展示页)

//
//  DetailViewController.m
//  InternetNews
//
//  Created by zhang peng on 13-4-16.
//
//

#import "DetailViewController.h"
#import "RootViewController.h"
#import "InternetNewsAppDelegate.h"


@implementation DetailViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 800)];
        label.numberOfLines = 10;
               
        InternetNewsAppDelegate *del = [(InternetNewsAppDelegate *)[UIApplication sharedApplication] delegate];
        NSLog(@"%@", self.view);
        
        [self.view addSubview:label];
        //label.text = del.des;
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"onClicked" object:nil];
    }
    return self;
}

- (void) test:(NSNotification*) notification
{
    NSLog(@"%@", notification);
    label.text = notification.object;
    [label sizeToFit];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值