PDA

View Full Version : XML parsing issue in a newsreader application.




ildu
May 6, 2009, 07:35 AM
Hello! I'm in a bit of a bind and would love some help :).

I'm developing a newsreader application similar to the USA Today and New York Times apps. I'm a beginner at Objective-C, so my aim at this point is to make it as simple as possible.

I've built the application using the Elements app (http://developer.apple.com/iphone/library/samplecode/TheElements/index.html) as a basis. I've stripped out things that I won't use and added some things of my own. So far, I've used a static .plist to act as the database where the news articles are taken from. Anyway, I'm now at the point where I need to be able to fetch the articles from an XML file on the web.

To accomplish this, I've tried combining my app with the XML Parsing app (http://www.iphonesdkarticles.com/2008/11/parsing-xml-files.html), but I've run into some trouble. As in the XML Parsing app, I've created an XMLParser class that handles the parsing. The news articles are fetched using a class called "Article", and the parsing seems to work on some level. The only issue is how to get the parsed values to assign the Article class. As far as I can tell, I need to assign the data source from another class called "Articles".

This method in the Articles class assigns the data source:

- (void)setupArticlesArray
{
NSDictionary *eachArticle;

// create a dictionary that contains the arrays of article data
self.articlesDictionary = [NSMutableDictionary dictionary];

// read the article data from the plist
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Articles" ofType:@"plist"];
NSArray *rawArticlesArray = [[NSArray alloc] initWithContentsOfFile:thePath];

// iterate over the values in the raw articles dictionary
for (eachArticle in rawArticlesArray)
{
// create an article instance for each
Article *anArticle = [[Article alloc] initWithDictionary:eachArticle];

// store that item in the articles dictionary with the name as the key
[articlesDictionary setObject:anArticle forKey:anArticle.otsikko1];

// release the article, it is held by the various collections
[anArticle release];
}
// release the raw article data
[rawArticlesArray release];

self.articlesSortedbyID = [self presortArticlesbyID];
}

And this method in the app delegate initiates the parsing:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// parse the content from the XML feed
NSURL *url = [[NSURL alloc] initWithString:@"http://users.metropolia.fi/~ilarim/articles.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate
[xmlParser setDelegate:parser];
}

As far as I can see, the issue is with the one line in each method that defines the actual source. All the other dictionary and array mumbo jumbo is irrelevent to the source.

So how do I replace this:
// read the article data from the plist
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Articles" ofType:@"plist"];

with this:
// parse the content from the XML feed
NSURL *url = [[NSURL alloc] initWithString:@"http://users.metropolia.fi/~ilarim/articles.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

Please please please help me! I have about a week to get the XML parsing done, or I don't graduate :eek:!