xmlparsing

xmlparsing

in .h file NSXMLParserDelegate

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

NSMutableDictionary *titledict;
NSMutableDictionary *descdict;
NSMutableDictionary *linkdict;
NSMutableDictionary *pubdatedict;
NSMutableString *currentElementValue;

.m file

- (void)viewDidLoad {
    [super viewDidLoad];


    //appDelegate = (rss2AppDelegate *)[[UIApplication sharedApplication] delegate];

    self.navigationItem.title = @"rssfeed";


    //NSURL *url = [[NSURL alloc] initWithString:@"http://feeds.bbci.co.uk/news/world/asia_pacific/rss.xml"];
    NSURL *url=[[NSURL alloc] initWithString:@"http://feeds.feedburner.com/quotationspage/qotd"];








    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    //xmlparser *parser = [[xmlparser   alloc] initxmlparser];

    [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);



    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (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"]];
    
    
    
    //NSString *str1=[descdict valueForKey:@"description"];
    
    
    //NSLog(@"str1 = %@",str1);
    
    
    //NSArray *firstSplit = [str1 componentsSeparatedByString:@"<p><"];
    
    
    //NSLog(@"0 index =  %@",[firstSplit objectAtIndex:0]);
    
    
    [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"]; } }
View Answers









Related Tutorials/Questions & Answers:
XmlParsing in Android
XmlParsing in Android  how can i XmlParsing in Android
xmlparsing
Advertisements
xml Converting to java using JDOM
.build("C:\\Users\\h.khammash\\Desktop\\Studening workspace\\XMLParsing
Read XML using Java
/xmlParsing/java-xml-parsing-using-sax.shtml http://www.roseindia.net/xml/sax

Ads