Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design/Implementation advice on XML Parsing with multiple connections/feeds/views

Starting my first iOS project and wanted to advice on how to structure the application. The app pulls a XML feed, parses it out and displays a list representing the items in the XML feed. When clicking on a item in the list the app will pull a new XML feed using one of the attributes from the previously pulled XML feed. This happens several layers of pull, parse, display and on user selection do the same thing over again. Now most of the XML element structure is something like this:

(These are simple examples just to demonstrate what's going on)

  • http://site.com/get/items/

returns (Display info on new view):

<items>
    <item id="123" name="item 1" />
    <item id="124" name="item 2" />
    <item id="125" name="item 3" />
</itmes>
  • http://site.com/get/description/123 (Example user has selected item 1, make call to get description)

returns:

<itemDescription>
    <description itemId="123" name="desc 1" description="blah 1" />
</itemDescription>

Wanted to know:

  • Should I have a connection class/object or a new connection in each view?
  • Should I have a parser class/object or parse the XML feed in each view?
  • I'm also looking to store some of the data returned so I don't need to call the XML feed again if the user navigates back to the main items list, but I would need to parse the itemsDescription XML feed every time.

I've looked at several tutorials on parsing XML and I get the gist of how to do this, wanting to focus more of the design and reusability instead of duplicating the code over in each new view. Or am I way off on how this works

like image 769
Phill Pafford Avatar asked Oct 27 '11 00:10

Phill Pafford


People also ask

Is XML easy to parse?

Well parsing XML is not an easy task. Its basic structure is a tree with any node in tree capable of holding a container which consists of an array of more trees.

What is XML parsing in Android?

Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document.

What does an XML parser take as input?

The goal of a DOM parser is to take an input–a string that contains an XML document–and produce a tree of objects that represents the same document in memory. A parser typically consists of two stages: a lexer and a parser. A lexer is given an input character stream and produces a token stream.


1 Answers

The best way you can do this following Apple Guidelines is checking one of their examples, some months ago I made an app similar to yours following this example. Also you can see how to make your app in offline mode.

Basic structure (w/o offline mode):

The SeismicXML sample application demonstrates how to use NSXMLParser to parse XML data. When you launch the application it downloads and parses an RSS feed from the United States Geological Survey (USGS) that provides data on recent earthquakes around the world. It displays the location, date, and magnitude of each earthquake, along with a color-coded graphic that indicates the severity of the earthquake. The XML parsing occurs on a background thread using NSOperation and updates the earthquakes table view with batches of parsed objects.

Advanced structure (with offline mode):

Demonstrates how to use Core Data in a multi-threaded environment, following the first recommended pattern mentioned in the Core Data Programming Guide.

Based on the SeismicXML sample, it downloads and parses an RSS feed from the United States Geological Survey (USGS) that provides data on recent earthquakes around the world. What makes this sample different is that it persistently stores earthquakes using Core Data. Each time you launch the app, it downloads new earthquake data, parses it in an NSOperation which checks for duplicates and stores newly founded earthquakes as managed objects.

For those new to Core Data, it can be helpful to compare SeismicXML sample with this sample and notice the necessary ingredients to introduce Core Data in your application.

Regarding cwieland answer I wouldn't use ASIHTTPRequest because is outdated, so if you want to follow his approach I would recomend you to use AFNetworking, where you can handle an XML request easy and fast:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
  XMLParser.delegate = self;
  [XMLParser parse];
} failure:nil];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
like image 162
3lvis Avatar answered Sep 20 '22 17:09

3lvis