Thursday, 5 January 2012

Parsing the Below XML.

Make a NSObject class.
Add the following codes in the .h file

@interface XmlEle : NSObject {
    NSString *title;
    NSString *description;
    NSString *imageurl;
}
@property (nonatomic, retain) NSString *title,*description,*imageurl;
@end

//in .m

@implementation XmlEle
@synthesize title,description,imageurl;

-(void) dealloc{
    [title release];
    title=nil;
    [description release];
    description=nil;
    [imageurl release];
    imageurl=nil;
    [super dealloc];
    }
@end


//In View Controller .h

@interface RootViewController : UITableViewController {
    NSXMLParser *parser;
    NSMutableString *currentAttribute;
    NSMutableArray *xmlElementObjects;
    XmlEle *tempEle,*eleme;
    NSString *ggg;
    NSMutableArray *hhh;
}
@property (nonatomic, retain) NSXMLParser *parser;
@property (nonatomic, retain) NSMutableString *currentAttribute;
@property (nonatomic, retain) XmlEle *tempEle,*eleme;
@property (nonatomic, retain) NSMutableArray *xmlElementObjects;
@end

//in .m

@implementation RootViewController
@synthesize parser,currentAttribute, xmlElementObjects,tempEle,eleme;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"My Parser";
    hhh=[[NSMutableArray alloc]init];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
    if(xmlElementObjects !=nil)
    {
        [xmlElementObjects release];
    }
    xmlElementObjects = [[NSMutableArray alloc] init];
   
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSData *xml = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"file:///Users/sweans/Dropbox/iPhone/Xmls/MyxmlMOdi.xml"]];
    self.parser=[[NSXMLParser alloc]initWithData:xml];
    [self.parser setDelegate:self];
    [self.parser parse];
    [self.parser release];
    self.parser=nil;
   
    [self.tableView reloadData];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
   
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if(![elementName compare:@"item"])
    {
tempEle = [[XmlEle alloc] init];          
     }
    else if(![elementName compare:@"title"])
    {
        currentAttribute = [NSMutableString string];
    }
    else if(![elementName compare:@"description"])
    {
        currentAttribute = [NSMutableString string];
    }
    else if(![elementName compare:@"media:thumbnail"])
    {
        ggg =[attributeDict valueForKey:@"url"];
        [hhh addObject:ggg];
   }
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   
    if(![elementName compare:@"item"])
    {
        [xmlElementObjects addObject:tempEle];
        [tempEle release];
        tempEle=nil;
    }
   
    else if(![elementName compare:@"title"])
    {
        tempEle.title=currentAttribute;
        currentAttribute=nil;
    }
   
       
    else if(![elementName compare:@"description"])
    {
        tempEle.description=currentAttribute;
        currentAttribute=nil;
    }
   
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(currentAttribute){
    [self.currentAttribute appendString:string];}
   
}

//Where we want the data to be displayed!!Here in the table view cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
   
    // Configure the cell.
//cell.textLabel.text=@"Me";
   
    eleme = [xmlElementObjects objectAtIndex:indexPath.row];
    NSString *Title= [eleme.title stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
    NSString *Descree= [eleme.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
   
    cell.textLabel.text=Title;
    cell.detailTextLabel.text=Descree;
       
   
   
       
        NSString* imageURL =[hhh objectAtIndex:indexPath.row];
        NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
        UIImage* imagee = [[UIImage alloc] initWithData:imageData];
        cell.imageView.image= imagee;
   
   
    return cell;
}


No comments:

Post a Comment