开发者

Check if an XML document is empty using NSXMLParser

开发者 https://www.devze.com 2022-12-15 20:17 出处:网络
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

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/>

T开发者_StackOverflow社区his 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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消