how to parse the image using NSMutableDictionary? - objective-c

I need help for parsing the image using dictionary. i can get the text data using dictionary but can't parse the image of the corresponding content using dictionary here my code
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"title"]) {
item = [[NSMutableDictionary alloc] init];
ktitle = [[NSMutableString alloc] init];
kpubdate = [[NSMutableString alloc] init];
kitem = [[NSMutableString alloc] init];
//kmedia = [[UIImageView alloc] init];
klink = [[NSMutableString alloc] init];
kdescription = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"title"]) {
[item setObject:ktitle forKey:#"title"];
[item setObject:klink forKey:#"link"];
[item setObject:kdescription forKey:#"description"];
[item setObject:kpubdate forKey:#"pubDate"];
//kmedia = [[UIImageView alloc] initWithImage:[UIImage keyPathsForValuesAffectingValueForKey:#"media:content"]];
//[item setObject:kmedia forKey:#"media:content"];
[arrayList addObject:[item copy]];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([currentElement isEqualToString:#"title"]){
[ktitle appendString:string];
}
else if ([currentElement isEqualToString:#"link"]) {
[klink appendString:string];
}
else if([currentElement isEqualToString:#"description"]){
[kdescription appendString:string];
}
}

finally i found answer small mistake is I parse the tag instead of url
Thanks for the help

Related

Get image url from RSS Data

I have got a RSS parsing project.it's my RSS url;
http://vimeo.com/udhdhmi/videos/rss
I'm parsing with NSXMLParser methods.
- (void)rssParseStart{
NSURL *url = [NSURL URLWithString:#"http://vimeo.com/udhdhmi/videos/rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if ([element isEqualToString:#"item"]) {
model = [[VideoModel alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:#"title"]) {
[model.videoTitle appendString:string];
}
if ([element isEqualToString:#"pubDate"]) {
[model.videoPubDate appendString:string];
}
if ([element isEqualToString:#"link"]) {
[model.videoLink appendString:string];
}
if ([element isEqualToString:#"description"]) {
[model.videoDescription appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[feeds addObject:model];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
[self.tableView reloadData];
}
No problem up to this point but coming datas of from "Description" tags thus;
-example;
<p><img src="http://i.vimeocdn.com/video/491445051_200x150.jpg" alt="" /></p><p><p class="first"></p></p><p><strong>Cast:</strong> DHMI</p><p><strong>Tags:</strong> </p>
I want only image url adress from that data.
thank for your interest.
you can get url from that data by trimming the string value you get from xml parser
// it is assumed that 'string' variable holds your current data
NSRange range;
int index;
range = [string rangeOfString:#"img src=\""];
index = range.location + range.length;
NSString* trimmedString = [string substringFromIndex:index];
range = [trimmedString rangeOfString:#"\""];
index = range.location + range.length - 1;
NSString* finalValue = [trimmedString substringToIndex:index];
finalValue stores the url as a string
Solution:
- (void)rssParseStart{
self.progressBar = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.progressBar.labelText = #"Lütfen bekleyiniz...";
NSURL *url = [NSURL URLWithString:#"http://vimeo.com/udhdhmi/videos/rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if ([element isEqualToString:#"item"]) {
model = [[VideoModel alloc]init];
}
if ([element isEqual:#"media:thumbnail"]) {
NSString *imageURLString = [attributeDict objectForKey:#"url"];
[model.videoDescription appendString:imageURLString];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:#"title"]) {
[model.videoTitle appendString:string];
}
if ([element isEqualToString:#"pubDate"]) {
[model.videoPubDate appendString:string];
}
if ([element isEqualToString:#"link"]) {
[model.videoLink appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[feeds addObject:model];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
[self.tableView reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}

Can't populate UITableView with data from XML (Russian text does not appear in slog like it should)

i can't figure out why i can't see a proper text when parsing XML page. My code is working but, when i try to output in NSLog, i can see following:
{
description = "\U0429\U0435\U043d\U043e\U043a \U0432 \U0434\U0430\U0440. \U041f\U0430\U043f\U0430 - \U043e\U0432\U0447\U0430\U0440 \U0412\U0415\U041e. \U041c\U0430\U043c\U0430 - \U043c\U0435\U0442\U0438\U0441 \U043b\U0430\U0431\U0440\U0430.";
}
I already did parse XML from example populated with English letters, so why i can't see a Russian text here? Or maybe it is not about the text and i made some mistakes?
There is a code from my NSObject class i use to get data from:
#implementation requestManager
-(id)initWithDelegate:(id<requestDelegate>)delegateObject{
self = [super init];
if (self){
self.delegate = delegateObject;
self.contentData = [NSMutableData data];
self.listOfItems = [NSMutableArray array];
}
return self;
}
-(void)loadXmlData{
NSURL *urlString = [NSURL URLWithString:#"http://*************url address**********"];
NSURLRequest *req = [NSURLRequest requestWithURL:urlString];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.contentData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *parserObj = [[NSXMLParser alloc]initWithData:self.contentData];
parserObj.delegate = self;
[parserObj parse];
}
#pragma mark - XML PARSER DELEGATE METHODS
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"description"]) isDescription = YES;
if ([elementName isEqualToString:#"text"]) isText = YES;
if ([elementName isEqualToString:#"name"]) isName = YES;
if ([elementName isEqualToString:#"images"]){
self.currentPosition = [NSMutableDictionary dictionary];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"description"]) isDescription = NO;
if ([elementName isEqualToString:#"text"]) isText = NO;
if ([elementName isEqualToString:#"name"]) isName = NO;
if ([elementName isEqualToString:#"images"]){
[self.listOfItems addObject:self.currentPosition];
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(#"%#", self.listOfItems);
}
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string{
if (isDescription) [self.currentPosition setValue:string forKey:#"description"];
if (isText) [self.currentPosition setValue:string forKey:#"text"];
if (isName) [self.currentPosition setValue:string forKey:#"name"];
}

Memory leaks while using NSXMLParser

I am having some difficulty dealing with memory leaks in the following code.
Using the leaks instrument within XCode, which shows up the memory leaks within some of my code that is used for rss parsing.
I am using XCode 4, and releasing the allocations at the foot of the code. I have tried adding releases to each local section which causes crashes or the program to stop working.
Any help of advice much appreciated!!
The code which causes the leaks:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to download story feed from web site (Error code %i )", [parseError code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) {
item = [[NSMutableDictionary alloc] init];
currentImage = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentImage forKey:#"media"];
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([currentElement isEqualToString:#"media"]) {
[currentImage appendString:string];
} else if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
}
And the releasing later on:
- (void)dealloc {
[currentElement release];
[rssParser release];
[stories release];
[item release];
[currentImage release];
[currentTitle release];
[currentDate release];
[currentSummary release];
[currentLink release];
[super dealloc];
}
Change the following:
UIAlertView * errorAlert = [[[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
This code should be executed only once:
item = [[NSMutableDictionary alloc] init];
currentImage = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
At the start of each didStartElement: method new instances are boing created yet only releases once when the class is dealloc'ed. Thus, as suing that didStartElement: is called more than once there is a build-up of instances of the string objects.
Probably what you want is to create these instances once at the instantiation of the class and then append to them as elements are encountered.
In any event release for each allocation.

UITable view,loading images from rss feed

I m reading the xml images from rss feed and parsing it in UITable view. Everything works fine, but it takes time to load the image content in the table view. The screen remains frozen. I'm using NSXMLParser to parse the image. Could you guys help me out, I'd be really greateful. Below is the code.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict copy];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
//currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"url"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"content:encoded"]) {
[currentSummary appendString:string];
}
}
NSString *imagefile1 = [[stories objectAtIndex:indexPath.row]objectForKey:#"url"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
cell.imageView.image=image1;
[image1 release];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines=2;
cell.textLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"title"];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"pubDate"];
Use Lazy Loading to load images....
somewhat dated but should put you in the right direction
http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/

UITable view,images,rssfeeds

i m using nsxmlparser to read the apple itunes rss feed..could u guys help to read this particular xml image.
<im:image height="55">http://a1.phobos.apple.com/us/r1000/028/Music/5c/aa/fe/mzi.fsnbyjmf.55x55-70.jpg</im:image>
below is the code
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*) attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"href"])
{
currentString=[attributeDict objectForKey:#"href"];
NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"href"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"entry"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"published"];
//[item setObject:currentImage forKey:#"im:image height=55"];
NSLog(#"the current image content:%#",item);
[stories addObject:[item copy]];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"published"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"url"]) {
[currentContent appendString:string];
}
}
#user652878 try this coding its works well..... In didStartelement, write as follows.. else if([elementName isEqualToString:#"media:content"])
{
currentImage = [attributeDict valueForKey:#"url"];
}
In didEndElement, ....... else if([elementName isEqualToString:#"media:content"]){
[item setObject:currentImage forKey:#"image"];
}
In foundcharacters........................ else if([currentImage isEqualToString:#"media:content"])
{
[currentImage appendString:string];
}
use NSLog to see that we get image link or not.... Sure u will get ur solution...