How to implement xml parsing in iphone ? -RV

How to implement xml parsing in iphone ? -RV

In .h file

@interface RootViewController : UITableViewController<NSXMLParserDelegate>{

    IBOutlet UITableView *page1;

    NSMutableArray *titlearray;
    NSMutableArray *descarray;
    NSMutableArray *linkarray;
    NSMutableArray *pubdatearray;

    NSMutableDictionary    *titledict;
    NSMutableDictionary    *descdict;
    NSMutableDictionary    *linkdict;
    NSMutableDictionary    *pubdatedict;


    NSMutableArray *rss;
    NSMutableDictionary  *item;
    NSMutableString *currentElementValue;
}
@property (nonatomic, retain) NSMutableDictionary *item;
@property (nonatomic, retain) NSMutableString *title;
@property (nonatomic, retain) NSMutableString *description;
@property (nonatomic, retain) NSMutableString *link;
@property (nonatomic, retain) NSMutableString *pubDate;

In .m file

@implementation RootViewController
@synthesize item,title,description,link,pubDate;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[[NSURL alloc] initWithString:@"http://feeds.feedburner.com/quotationspage/qotd"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];
     BOOL success = [xmlParser parse];
     if(success)
        NSLog(@"No Errors");
    else
        NSLog(@"Error Error Error!!!");

NSLog(@"titlearray--%@",titlearray);
    NSLog(@"descarray--%@",descarray);
    NSLog(@"linkarray--%@",linkarray);
    NSLog(@"pubdatearray--%@",pubdatearray);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   // return 10;
    [titlearray count];
}
cell for at index
    cell.textLabel.text=[titlearray objectAtIndex:indexPath.row];



- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"channel"]) {
        //Initialize the array.
        titlearray=[[NSMutableArray alloc]init];
        descarray=[[NSMutableArray alloc]init];
        linkarray=[[NSMutableArray alloc]init];
        pubdatearray=[[NSMutableArray alloc]init];

        //appDelegate.rss = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"item"]) {

        titledict=[[NSMutableDictionary alloc]init];
        descdict= [[NSMutableDictionary alloc]init];
        linkdict=[[NSMutableDictionary alloc]init];
        pubdatedict=[[NSMutableDictionary alloc]init];}}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    if(!currentElementValue)
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
    //    [currentElementValue appendString:string];
        currentElementValue=[NSMutableString stringWithString:string];
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"item"]) {
        [titlearray addObject:[titledict valueForKey:@"title"]];
        [descarray addObject:[descdict valueForKey:@"description"]];
[linkarray addObject:[linkdict valueForKey:@"link"]];
        [pubdatearray addObject:[pubdatedict valueForKey:@"pubDate"]];
}
else if([elementName isEqualToString:@"title"])
    {
        [titledict setObject:currentElementValue forKey:@"title"];}
else if([elementName isEqualToString:@"description"])
    {
        [descdict setObject:currentElementValue forKey:@"description"];}
else if([elementName isEqualToString:@"link"])
    {
        [linkdict setObject:currentElementValue forKey:@"link"];}
else if([elementName isEqualToString:@"pubDate"])
    {
        [pubdatedict setObject:currentElementValue forKey:@"pubDate"];
}}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    nextview *n1=[[nextview alloc]initWithNibName:@"nextview" bundle:nil];
    //n1.temparray=[[titlearray objectAtIndex:indexPath.row] valueForKey:@"title"];
    n1.temparray=[titlearray objectAtIndex:indexPath.row];
    n1.temparray1=[linkarray objectAtIndex:indexPath.row];
    n1.temparray2=[pubdatearray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:n1 animated:YES];
    [nextview release];   
}

// In nextview.h

@interface nextview : UIViewController {
    IBOutlet UITableView *page2;
    IBOutlet UILabel *l1;
    IBOutlet UILabel *l2;
    IBOutlet UILabel *l3;
}
@property (nonatomic, retain) NSMutableArray *temparray;
@property (nonatomic, retain) NSMutableArray *temparray1;
@property (nonatomic, retain) NSMutableArray *temparray2;
@end

// IN nextview.m

@synthesize temparray,temparray1,temparray2;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"description";
    l1.text=temparray;
    l2.text=temparray1;
    l3.text=temparray2;
}
View Answers

January 8, 2012 at 3:53 PM

??









Related Tutorials/Questions & Answers:

Ads