iPhone XML Parsing problem - iphone

I am trying to extract the title from this xml:
<entry>
...
<title type="html">Some title</title>
<published>2011-02-07T00:04:16Z</published>
<updated>2011-02-07T00:04:16Z</updated>
...
</entry>
in the NSXMLParsing's didStartElement code:
if ([elementName isEqualToString:#"entry"])
{
item = [[NSMutableDictionary alloc] init];
}
if([elementName isEqualToString:#"title"])
{
currentElement = [elementName copy];
currentTitle = [[NSMutableString alloc] init];
}
in foundCharacters:
if ([currentElement isEqualToString:#"title"])
{
[currentTitle appendString:string];
}
in didEndElement:
if ([elementName isEqualToString:#"entry"])
{
[item setObject:currentTitle forKey:#"title"];
[currentElement release];
currentElement = nil;
[item release];
item = nil;
}
The problem is that for some reason when it gets to the didEndElement the currentTitle has got the three node's content, so its:
Some
title2011-02-07T00:04:16Z2011-02-07T00:04:16Z
I don't get why its picking up the published and updated node and appending them to the title string.

You set currentElement to #"title" in didStartElement: but you never unset it. The first element in this particular XML file is title, so you set currentElement and for every foundCharacters: thereafter you append them. Change didStartElement: to:
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"])
{
item = [[NSMutableDictionary alloc] init];
}
if([elementName isEqualToString:#"title"])
{
currentTitle = [[NSMutableString alloc] init];
}

You have to add to didEndElement:
if ([elementName isEqualToString:#"title"])
{
[currentElement release];
currentElement = nil;
}

define
NSMutableString *strFoundCharacters; in class method set property and synthesize it.
then in
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
NSString *strAppend = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([strAppend length] > 0) {
[self.strFoundCharacters appendString:string];
}
}
make nil to strfoundcharacter in
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"title"] ) {
self.currentTitle = self.strFoundCharacters;
}
[self.strFoundCharacters setString:#""];
}
i think it will work

Related

NSXMLParser how to pass the NSMutableDictionary to a NSMutableArray

I would like to pass the nsdictionary I am creating into an nsmutablearray but I'm not sure when or how to do it in the nsxmlparser delegates.
this is what I have done so far
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"item"]) {
valueDictionary = [[NSMutableDictionary alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSMutableString *dicString = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
currentElement = dicString;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"title"]) {
titleString = currentElement;
[self.valueDictionary setObject:titleString forKey:#"title"];
NSLog(#"%#", [valueDictionary objectForKey:#"title"]);
NSLog(#" ");
currentElement = nil;
}
if ([elementName isEqualToString:#"description"])
{
descriptionString = currentElement;
[self.valueDictionary setObject:descriptionString forKey:#"description"];
NSLog(#"%#", [valueDictionary objectForKey:#"description"]);
NSLog(#" ");
currentElement = nil;
}
In -parser:didEndElement:namespaceURI:qualifiedName:, listen for the end of the item element, then add valueDictionary to a mutable array instance on your class.
if ([elementName isEqualToString:#"item"])
{
[self.mutableArrayOfDictionaries addObject:self.valueDictionary];
}
if ([elementName isEqualToString:#"title"]) {
titleString = currentElement;
[self.valueDictionary setObject:titleString forKey:#"title"];
NSLog(#"%#", [valueDictionary objectForKey:#"title"]);
NSLog(#" ");
currentElement = nil;
}
if ([elementName isEqualToString:#"description"])
{
descriptionString = currentElement;
[self.valueDictionary setObject:descriptionString forKey:#"description"];
NSLog(#"%#", [valueDictionary objectForKey:#"description"]);
NSLog(#" ");
currentElement = nil;
}

Parse 2 RSS feeds in app delegate

I'm following a tutorial and it's working fine but if I wanted to parse 2 RSS feeds it seems to overwrite one array instead of saving them in the respective arrays.
This is in my delegate:
NSURL *url2 = [[NSURL alloc] initWithString:#"myRSSFEED1"];
NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithContentsOfURL:url2];
//Initialize the delegate.
XMLParser1 *parser1 = [[XMLParser1 alloc] initXMLParser];
//Set delegate
[xmlParser1 setDelegate:parser1];
//Start parsing the XML file.
BOOL successs = [xmlParser1 parse];
if(successs)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
//VIDS
NSURL *url = [[NSURL alloc] initWithString:#"MYRSSFEED2"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
that parses feed 1, allocates it to the array then parses 2 and seems to overwrite the first insteaf of using the second array that are defined as
#synthesize pics;
#synthesize books;
And saved in my XMLParser & XMLParser1
I can't figure out how to stop it from overwriting.
Here is my XMLParsers too:
- (void)parsers:(NSXMLParser *)parsers didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"Books"]) {
//Initialize the array.
appDelegate2.pics = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"]) {
//Initialize the book.
apics = [[BookPhoto alloc] init];
//Extract the attribute here.
apics.bookID = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading HAVid value :%i", apics.bookID);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parsers:(NSXMLParser *)parsers didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:#"Book"]) {
[appDelegate2.pics addObject:apics];
[apics release];
apics = nil;
}
else
[apics setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
And my XMLParser.m
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"Books"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"]) {
//Initialize the book.
aBook = [[Book alloc] init];
//Extract the attribute here.
aBook.bookID = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading id value :%i", aBook.bookID);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:#"Book"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
Any help on this would be brilliant,
I suggest putting a breakpoint at the point where the array is not getting loaded, and seeing if (1) it even gets called, and (2), whether it is saving the data into the array at all.
my bad
seems i had extra s's in parser:
- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
should have been:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
cheers for the help tho

NSxml parser upto 3 levels on iphone

i have a issue getting the value of A->1,2,3,4 & b->1,2,3,4
i have to show these data on 3 tables
table 1) A , B
table 2)when user click on A -> he can see 1,2,3,4 in tablular form
but i my case i can only show the last value of A i.e 4 ( not 1,2,3)
how to parse all 1,2,3,4
<?xml version="1.0" encoding="UTF-8"?>
<hall>
<movie Name="A">
<stall Name="1">
<description>qwe.</description>
<rating>5</rating>
</stall>
<stall Name="2">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="3">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="4i">
<description>wer.</description>
<rating>5</rating>
</stall>
</movie >
<movie Name="B">
<stall Name="t1">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="2">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="3">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="4">
<description>wer.</description>
<rating>5</rating>
</stall>
</movie>
</hall>
#
UPDATES
#
my problem is i dont know how to separate Movie name A and B data
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"hall"])
{
movieList = [[NSMutableDictionary alloc]init];
appDelegate.books = [[NSMutableArray alloc]init];
appDelegate.HospN = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:#"movie"])
{
currentMovie = [[NSString alloc] initWithString:[attributeDict objectForKey:#"Name"]] ;
[appDelegate.books addObject:[attributeDict objectForKey:#"Name"]];
//[aBook.movie addObject:[attributeDict objectForKey:#"Name"]];
NSLog(#"Processing Value: %#", currentMovie);
//aBook.testament=currentMovie;
aBook.stall = [attributeDict objectForKey:#"name"];
NSLog(#"Movie A name : %#",[movieList objectForKey:#"A"]);
}
else if([elementName isEqualToString:#"stall"])
{
[appDelegate.HospN addObject:[attributeDict objectForKey:#"Name"]];
NSLog(#"Processing stall Value: %#", currentElementValue);
NSLog(#"appDelegate.HospN: %#", appDelegate.HospN);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
//NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"hall"])
{
//NSLog(#"Movie List : %#",movieList);
}
else if([elementName isEqualToString:#"movie"])
{
[movieList setObject:appDelegate.books forKey:currentMovie];
NSLog(#"Movie name : %#",appDelegate.books );
NSLog(#"Movie A name : %#",[movieList objectForKey:#"A"]);
[currentMovie release];
//[stallNames release];
currentMovie = nil;
//stallNames = nil;
}
else if([elementName isEqualToString:#"stall"])
{
//you can populate other attributes here if stall is not just string but a modal class.
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
my array my appDelegate.HospN: value is (
1,
2,
3,
4i,
t1,
2,
3,
4
)
**but i want to chop data at A= 1,2,3,4i
and B= t1,2,3,4**
Updtate
how to get the value of description and rating ??
else if([elementName isEqualToString:#"description"])
{
NSLog(#"desp List : \n%#\n",currentElementValue);// i can see all value but how to chop data based on A and B
[movieList setObject:currentLink forKey:#"description"];
[appDelegate.despArray addObject:[movieList copy]];
NSLog(#"adding story: %#",currentLink);
}
else if([elementName isEqualToString:#"rating"])
{
}
By observing the xml you will find the relationship like this...
A hall can have one or more movie and Each movies can have one ore more stall.
so what you can do is.
have a instance dictionary, say movieList, and an instance array say,stallNames, and one movieName,NSString in your xml parsing class.
movieList : will have mapping of movie name with their respective stalls.(NSMutableDictionary)
stallNames : will contain stalls related to particular movie.(NSMutablArray)
currentMovie: will have current movie name as it would be used as key in movieList Dictionary.(NSString)
Now in startElement method ...
if([elementName isEqualToString:#"hall"])
{
movieList = [[NSMutableDictionary alloc]init];
}
else if([elementName isEqualToString:#"movie"])
{
currentMovie = [[NSString alloc] initWithString:[attributeDict objectForKey:#"Name"]] ;
stallNames = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:#"stall"])
{
[stallNames addObject:[attributeDict objectForKey:#"Name"]];
}
in didEndElement method
if([elementName isEqualToString:#"hall"])
{
NSLog(#"Movie List : %#",movieList);
}
else if([elementName isEqualToString:#"movie"])
{
[movieList setObject:stallNames forKey:currentMovie];
[currentMovie release];
[stallNames release];
currentMovie = nil;
stallNames = nil;
}
else if([elementName isEqualToString:#"stall"])
{
//you can populate other attributes here if stall is not just string but a modal class.
}
Note : as in above code posted by you , you have a Book class, and it seems it represents Movie, so what you can do is, have another property which is array type which will hold stall names. and this Book object will be added later on an array(in didEndElement when you will encounter elementName "movie"). make this array as instance variable and initialize it when you encounter "hall" tag in startElement.
UPDATE ANS(AS PER NEW CODE)
//declare following instance variables. I am leaving aBook variable because I am not sure of its use.
NSMutableDictionary *movieList;
NSMutableArray *stallArray;
NSMutableString *currentMovieKey;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"hall"])
{
movieList = [[NSMutableDictionary alloc]init]; // it will contain list of all the movies in the xml.
}
else if([elementName isEqualToString:#"movie"])
{
currentMovieKey = [[NSMutableString alloc] initWithString:[attributeDict objectForKey:#"Name"]] ; // will be used for setting key for stallArray.
stallArray= [[NSMutableArray alloc] init]; // it will contain stall names.
}
else if([elementName isEqualToString:#"stall"])
{
[stallArray addObject:[attributeDict objectForKey:#"Name"]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"hall"])
{
//parsing finished...end of root tag. movieList will be the final dictionary that will have your data
NSLog(#"Movie List : \n%#\n",movieList);
}
else if([elementName isEqualToString:#"movie"])
{
[movieList setObject:stallArray forKey:currentMovieKey];
[currentMovieKey release];
currentMovie = nil;
[stallArray release];
stallArray = nil;
}
else if([elementName isEqualToString:#"stall"])
{
//you can populate other attributes here if stall is not just string but a modal class.
}
//I was not sure about your else part...
[currentElementValue release];
currentElementValue = nil;
}
Thanks,

Extracting name of RSS feed in Cocoa-Touch

I am trying to parse an RSS XML feed. I have figured out how to parse what's in the tags for the individual stories, but I cannot figure out how to get the name of the entire feed (for example "CNN's News Feed"). I think it's in and I've tried a ton of things but I can't figure it out. Below is part of my parsing code that I think what seemed like the most sense, but didn't work. Can anyone help?
Thanks!!
GL
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"] || [elementName isEqualToString:#"channel"]) {
item = [[NSMutableDictionary 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:#"channel"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[feedsArray addObject:[item copy]];
}
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
NSLog(#"adding story: %#", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
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];
}
}
The feed name is always included in the name of title by " - " at the end. Now if you want to get the feed name you can do it like.
NSDictionary *feedDict = [feedArray objectAtIndex:indexPath.row];
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:1];
NSArray *tempArr = [[NSArray alloc] initWithArray:[[feedDict valueForKey:#"title"] componentsSeparatedByString:#" - "]];
[lbl setText:[tempArr objectAtIndex:0]];
So you will see the name of feed in the lable.
Hope this helps.
Thanks,
Madhup
The feeds title lays outside items and channels so you should also trigger for a title in didStartElement. You also might need to make some logic to differentiate between titles within items and channels and the global title.
Let's have a try to fix it...(also noticed some possible memory leaks)
Not tested
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
if (currentElement)
[currentElement release];
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"] ||
[elementName isEqualToString:#"channel"]) {
if (item)
[item release];
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
} else if (!item && [elementName isEqualToString:#"title"]) {
[currentElement release];
currentElement = [#"<GlobalTitle>" copy];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"channel"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[feedsArray addObject:item]; // item will be retained by array
[item release];
[currentTitle release]; // these are also retained by the dictionary
[currentLink release];
[currentSummary release];
[currentDate release];
item = nil;
currentTitle = nil;
currentLink = nil;
currentSummary = nil;
currentDate = nil;
}
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:item]; // item will be retained by array
[item release];
[currentTitle release]; // these are also retained by the dictionary
[currentLink release];
[currentSummary release];
[currentDate release];
item = nil;
currentTitle = nil;
currentLink = nil;
currentSummary = nil;
currentDate = nil;
NSLog(#"adding story: %#", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string {
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:#"<GlobalTitle>"]) {
self.feedTitle = string; // The feeds title is saved here
}
}
I've released an open source RSS/Atom Parser for iPhone and it makes reading and parsing web feeds extremely easy.
You can set it to read just the feed info (title, summary, etc) if you want. Checkout the documentation and play around with the demo app. Hope this helps!

Using NSXMLParser with CDATA

I'm parsing an RSS feed with NSXMLParser and it's working fine for the title and other strings but one of the elements is an image thats like
<!CDATA <a href="http:image..etc>
How do I add that as my cell image in my table view? Would I define that as an image type?
This is what i'm using to do my parsing:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName 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];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
NSLog(#"adding story: %#", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...
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];
}
}
You can use
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
The point of CDATA is that anything within it is not treated as part of the XML document.
So if you have tags in CDATA the parser will ignore them.
I'm guessing this CDATA is in the description element. So you'll need to extract the tags from the "description" element, either manually or via another instance of a parser.