i am creating an app that shows the latest feeds from a particular webservice,i am using NSXMLParserDelegate protocol for this purpose,well i read the apple documentation and i tried some tutorials too,but something seems to be going wrong somewhere,i dont understand how does the didEndElement,foundCharacters work,anyways i want to display the image,title and content,pub-date of the post,i am newbie to xmlparsing here’s my viewcontroller.h(i have just parsed only the title element in the following code)
@property(nonatomic,strong)NSString *currentElement;
@property(nonatomic,strong)NSString *currentTitle;
@property(nonatomic,strong)NSMutableArray *titles;
viewdidload
NSURL *url=[NSURL URLWithString:@"http://www.forbes.com/fast/feed"];
NSXMLParser *parser=[[NSXMLParser alloc]initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
NSLog(@"%d",titles.count);
didStartElement
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
self.currentElement=elementName;
if ([self.currentElement isEqualToString:@"title"])
{
self.currentTitle=[NSMutableString alloc];
titles=[[NSMutableArray alloc]init];
titles=[attributeDict objectForKey:@"title"];
}
}
foundCharacters
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
self.currentElement=elementName;
if ([self.currentElement isEqualToString:@"title"])
{
self.currentTitle=[NSMutableString alloc];
titles=[[NSMutableArray alloc]init];
titles=[attributeDict objectForKey:@"title"];
}
}
didEndElement
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([self.currentElement isEqualToString:@"title"])
{
NSLog(@"%@",self.currentTitle);
}
}
doubts
1)where am i supposed to declare my titles array so that i can add individual title object to it.
what is the use of [attributeDict objectForKey] in didStartElement? it returned null for my program
2)what does foundCharacters delegate actually do? what does it append?
3)After didEndElement why doesnt the compiler reach for didStartElement and not the foundCharacters ?
4)finally should i actually use NSXMLParserDelegate protocol for the xml parsing,do others like touchXML,TBXML and others provided in the raywenderlich make a difference?
i am sorry for the long post,but i havent got any satisfying answers online regarding my queries,i used all the breakpoints and figured out how the delegates are called back and forth,i need some enlightening answers to my queries,thanks and sorry
1. Declare your array before starting to parse.Whenever you meet an elements (an xml tag), initialize the elements (set some BOOL in the class in a way that you can recognize what element you are reading);
2. Found characters are the characters found as value of a tag.If you know what element you are reading (reading your instance variables), you should append this string to your temporary NSMutableString and add it to the array only when the element is ended.
3. Because it doesn’t start finding other characters until a new tag is reached.
Example
I see that you are confused, let’s say that you have this XML code:
When you meet the tag the element starts, then you find other characters (not the entire string, just a part of the string) until the string ends, then when you meet the tag the element is ended.