Share on Google+Share on Google+

Ravi Vora
How to implement xml parsing in iphone ? -RV
1 Answer(s)      5 years and 3 months ago
Posted in : XML

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;
}

Ads

View Answers

January 8, 2012 at 3:53 PM


??










Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training were to start with xml, XSD and java write a program that Implement an integrated COM client server to add two numbers.by visuall studio 2010 Building a Parent Child relationship XML how i connect two page xml question struts cfg file for DispatchAction with mysql db Creating custom output using XMLUnit - MatchTracker duplicates the output data Related to XML Related to XML Reading child of child nodes in xml Reading multiple xml files Spliting Large XML and one by one Moved to Output Folder XMl and Java web services project download xml file from website using java code Download how to create an xml file in following clear format Conditional increment in xslt Conditional increment in xslt Conditional increment in xslt HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP? xml and servlets xml and servlets Question about parsing XML document with DOM in JAVA. Removal of XML header Converting PHP page with flash to JSP page without flash saving data in xml Doubt about Xstream Converter Reading xml file using dom parser in java with out using getelementby tag name xml dynamically how to change attiribute values in xml XML Schema xml xml sax parser for xml parsing xml using sax xml to html via java i want java code for this xml file...please show me.. SAX Parser for huge XML file xml parsing "opening video data error" in j2me XML to JSP mapview1 mapview searchbar searchbar jsonparsing xmlparsing retrieve multiple attribute values retrieve multiple attribute values

Ads

 
Advertisement null

Ads