views:

280

answers:

3

Im writing an iPhone app which retrieves data from a web service as XML. If no data is found by the web service, it returns an empty document like

<?xml version="1.0"?>
<root_element/>

This data is being parsed and then added to an array and subsequently a table view.

In my -numberOfRowsInSection I have return [self.array count]; But obviously if the document is empty, nothing is added to the array, and i dont get a parse error, but instead get an index out of bounds error. Is there a isDocumentEmpty method or some such means of checking to see if the document has elements?

How else would you suggest i check this? And what way should i use to alert the user? An alert view and then pop the controller?

Thanks.

A: 

You can use:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
   attributes:(NSDictionary *)attributeDict

and check for the "root_element/" as the elementName

ennuikiller
I tried using "root_element/" as the elementName but it didnt work, the elementName was still "root_element", which is present in a correct document as well.
joec
A: 

For how to use NSXMLParser to parse XML data you can check SeismicXML sample application from Apple.

In this application the XML parsing occurs on a background thread and updates the earthquakes Table view with batches of parsed objects.

raaz
A: 

I think the problem is where you want to reload the tableview. Out of bounds exception generates when you access an element from an array which is beyond the range of array indexes.

So here are some debugging tips to know exactly what your problem is

  • first of all go to console after the exception has occurred. type "where" and press enter you will get exception details.
  • You will get the exact line number where you are getting this error.
  • Put a break point just before that line and debug there

You will find the error you are doing.

If this does not help. Please show some of your code snippet.

Thanks,

Madhup

Madhup