Get image url from RSS Data - objective-c

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

Related

I can't get full text from tag parsing XML page

i have an xml tag <"text>", that have following text:
Федя. Он хороший, добрый, энергичный пёс, которому отчаянно не хватает
удачи.Никто уже, никогда не узнает, что случилось с Федей.. "wall of
text".8-910-444-59-42 Лариса
When i want to get that text out from that tag i only got last one, for that tag i got only "Лариса".
My question is how to fix it so i can see whole text? Maybe somehow that connected with "br" tag? All other tags extracted just fine, but not this..
#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:#"shortText"]) isShortText = YES;
if ([elementName isEqualToString:#"images"]) isImage = YES;
if ([elementName isEqualToString:#"announcement"]){
self.currentPosition = [NSMutableDictionary dictionary];
self.imageUrlString = nil;
}
if ([elementName isEqualToString:#"image"])
{
NSString *name = attributeDict[#"name"];
if ([name isEqualToString:#"prev"]&& self.imageUrlString == nil)
{
NSString *urlString = attributeDict[#"url"];
if (urlString)
{
// NSLog(#"%# found it", urlString);
self.imageUrlString = urlString;
[self.listOfImages addObject:self.imageUrlString];
}
}
}
}
-(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:#"shortText"]) isShortText = NO;
if ([elementName isEqualToString:#"images"]) isImage = NO;
if ([elementName isEqualToString:#"announcement"]){
[self.listOfItems addObject:self.currentPosition];
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
[self.delegate didFinishParsingToArray:self.listOfItems];
[self.delegate didFinishParsingToImages:self.listOfImages];
// NSLog(#"%#", self.currentPosition);
// NSLog(#"%# Its work", self.listOfImages);
}
-(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"];
if (isShortText) [self.currentPosition setValue:string forKey:#"shortText"];
}
Your problem in -(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string implementation. In this method you need append together found strings for each parsed tag, but in your implementation you set each found string and in fact - last of found string (this method can be called more than once for a single tag).
Change your code with something like below:
if (isText) {
if (currentPosition[#"text"])
{
NSMutableString *str = currentPosition[#"text"]
[str appendString:string];
}
else
{
[self.currentPosition setValue:[NSMutableString stringWithString:string] forKey:#"text"]
}
};
Note: You can get a sample problem XML? I want to test my XMLConverter on it.

Parsing an xml response using NSXml

I trying to parse an xml that has one root element with 2 child elements with the same name. However they keep getting concatenated together. How can i fix this? here is my code so far
-
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if([element isEqualToString:#"bustime-response"]){
self.item = [[NSMutableDictionary alloc]init];
self.direction =[[NSMutableString alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:#"dir"]){
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.direction appendString:string];
}
}
Try this it works for me:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
element = elementName;
if ([element isEqualToString:#"bustime-response"])
{
elementFound = YES;
if (self.item)
self.item = nil;
self.item = [[NSMutableDictionary alloc]init];
if (self.direction)
self.direction = nil;
self.direction =[[NSMutableString alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (elementFound)
{
if ([element isEqualToString:#"dir"]){
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.direction appendString:string];
}
//Add another if with the tag you want to retrieve the data from here
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([element isEqualToString:#"bustime-response"])
{
elementFound = NO;
// self.receivedDataArray - main array to store all of your dictionary
[self.receivedDataArray addObject: self.direction];
[self.receivedDataArray addObject: self.item];
self.direction = nil;
self.item = nil;
}
}
As you see you have to add another BOOL variable elementFound and NSMutableArray receivedDataArray for storing all of the data. Give it a go and let me know is it work for you. Remember it's good to add NSLogs for debugging, it helps a lot.

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

Trouble Parsing XML and fetching data

I have need to parse the following xml from webservicex.net, for and iPhone App. I need to store the verse Verse and BibleWords into some NSArray/NSDictionary or something.
<string xmlns="http://www.webserviceX.NET">
<NewDataSet>
<Table>
<Book>1</Book>
<BookTitle>Genesis</BookTitle>
<Chapter>1</Chapter>
<Verse>1</Verse>
<BibleWords>In the beginning God created the heaven and the earth.</BibleWords>
</Table>
<Table>
<Book>1</Book>
<BookTitle>Genesis</BookTitle>
<Chapter>1</Chapter>
<Verse>2</Verse>
<BibleWords>And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</BibleWords>
</Table>
<Table>
<Book>1</Book>
<BookTitle>Genesis</BookTitle>
<Chapter>1</Chapter>
<Verse>3</Verse>
<BibleWords>And God said, Let there be light: and there was light.</BibleWords>
</Table>
<Table>
<Book>1</Book>
<BookTitle>Genesis</BookTitle>
<Chapter>1</Chapter>
<Verse>4</Verse>
<BibleWords>And God saw the light, that it was good: and God divided the light from the darkness.</BibleWords>
</Table>
</NewDataSet>
</string>
I wrote the following code for parsing the data.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"string"]) {
model = [[Model alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue) {
currentElementValue = [[NSMutableString alloc]initWithString:string];
}
else {
[currentElementValue appendString:string];
}
NSLog(#"%#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"NewDataSet"]) {
return;
}
if ([elementName isEqualToString:#"Table"]) {
[verses addObject:model];
model = nil;
}
else {
[model setValue:currentElementValue forKey:elementName]; //The exception break point hits here
}
currentElementValue = nil;
}
The currentElementValue is displayed correctly. model is and object of a class named Model, currentElementValue is a NSMutableString object verses is an NSMutableArray. I am a beginner in objective C and haven't done parsing before. The problem is that the method:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
is not working as expected. The app got broken after hitting the exception break point that i have commented in the code. How can I get the value of Verse and BibleWords and store it to some NSArray/NSMutableArray/NSDictionary/NSMutableDictionary. Please ask if there is anything else I need to mention to see where the problem actually lies.
EDIT:
Tried with this:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
captureCharacters = NO;
[currentElementValue setString : #""];
if ([elementName isEqualToString:#"Table"]) { //set a break point here as well. The elementName is string and hence is not entering any of the conditions in this delegate method. Hence captureCharacters is always set to NO and nothing is working.
model = [[Model alloc]init];
}
else if ([elementName isEqualToString:#"Verse"]) {
capturingCharacters = yes;
}
else if ([elementName isEqualToString:#"BibleWords"]) {
capturingCharacters = yes;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(capturingCharacters) {
[currentElementValue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"Table"]) {
[array addObject : model];
model = nil;
}
else if ([elementName isEqualToString:#"Verse"]) { //set a break point here
model.Verse = currentElementValue; //not entering this condition
}
else if ([elementName isEqualToString:#"BibleWords"]) {
model.BibleWords = currentElementValue; //not entering this condition
}
}
It is not working and I am getting null values for model.Verse and model.BibleWords.
How does these methods work?
EDIT-2
Model Interface:
//model.h
#protocol HttpRequestHandler <NSObject>
- (void)returnFromSiteParsedData:(NSMutableArray *)parsedData;
#end
#interface Model : NSObject <NSURLConnectionDelegate, NSXMLParserDelegate>
{
NSString *urlToLoad;
NSMutableURLRequest *requestSiteToSendData;
NSURLConnection *connectionToSiteToSendData;
NSString *verseText;
int verseNumber;
}
#property(nonatomic, retain) NSString *verseText;
#property(nonatomic, assign) int verseNumber;
- (void)loadSiteToSendDataWithChapterNumber:(int)chapterNumber AndBookNumber:(int)bookNumber;
#property(nonatomic, retain)id<HttpRequestHandler>httpRequestHandlerDelegate;
#end
Model Implementation
//model.m
#implementation Model
#synthesize verseNumber;
#synthesize verseText;
- (void)loadSiteToSendDataWithChapterNumber:(int)chapterNumber AndBookNumber:(int)bookNumber {
urlToLoad = [[NSString alloc]initWithFormat:#"http://www.webservicex.net/BibleWebservice.asmx/GetBibleWordsByBookTitleAndChapter?BookTitle=Genesis&chapter=1"];
requestSiteToSendData = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString:urlToLoad] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
connectionToSiteToSendData = [[NSURLConnection alloc]initWithRequest:requestSiteToSendData delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Parse the XML into a dictionary
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
VBSParser *vbsParser = [[VBSParser alloc]initVBSParser];
[parser setDelegate:vbsParser];
BOOL success = [parser parse];
if (success) {
NSMutableArray *verses = [vbsParser verses];
NSLog(#"%#", verses);
NSLog(#"Number: %d\n Text: %#", verseNumber,verseText);
[self.httpRequestHandlerDelegate returnFromSiteParsedData:verses];
}
// Print the dictionary
}
#end
A delegate is used to return parsed data to the view controller.
Make a BOOL capturingCharacters;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
captureCharacters = NO;
[currentElementValue setString : #""];
if ([elementName isEqualToString:#"Table"]) {
model = [[Model alloc]init];
}
else if ([elementName isEqualToString:#"Verse"]) {
capturingCharacters = yes;
}
else if ([elementName isEqualToString:#"BibleWords"]) {
capturingCharacters = yes;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(capturingCharacters) {
[currentElementValue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"Table"]) {
[array addObject : model];
model = nil;
}
else if ([elementName isEqualToString:#"Verse"]) {
model.Verse = currentElementValue;
}
else if ([elementName isEqualToString:#"BibleWords"]) {
model.BibleWords = currentElementValue;
}
}

NSXMLParser can not get the content of elements correctly

i have the following XMLParser but when i try to run it, it doesn't work properly.
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:#"results"])
{
currentJob = [SearchResult alloc];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:#"jobTitle"])
{
currentJob.jobTitle = currentNodeContent;
}
if ([elementname isEqualToString:#"location"])
{
currentJob.shortAddress = currentNodeContent;
}
if ([elementname isEqualToString:#"companyName"])
{
currentJob.employer = currentNodeContent;
}
if ([elementname isEqualToString:#"results"])
{
[self.jobs addObject:currentJob];
currentJob = nil;
currentNodeContent = nil;
}
}
AND here is my foundCharakter Method:
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
The output doesn't start from the beginning, it starts from the middle of the String...
I just can not understand, why some results look nice where some others don't.
What am i doing wrong ? How can i parse an xml properly ?
Any help will be appreciated.
Thx in advance
I used the following code and works now:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(!elementContentString)
elementContentString = [[NSMutableString alloc] initWithString:string];
else
[elementContentString appendString:string];
}
I don't think there any problem in your code, it is working fine at my end. I am using the twitter api for getting xml and it is giving me proper output.