Return the count of an NSMutableArray from an XML id number in Objective-C?

荒凉一梦 提交于 2019-12-13 04:29:13

问题


I am building an NSMutableArray out of an XML file and need to return the count of each resident id in the XML file. Is it possible to do this?

<residents>
<resident id="1">
    <name>
        <first>Daffy</first>
        <last>Duck</last>
    </name>
</resident>

<resident id="2">
    <name>
        <first>Mickey</first>
        <last>Mouse</last>
    </name>
</resident>

etc...

I will be returning the count using code similar to this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Count = %i", [appDelegate.residents count]);
return [appDelegate.residents count];

Any suggestions?

For the array, in my AppDelegate.h I have:

    @interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    UINavigationController *navigationController;

    NSMutableArray *residents; 
}


@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *residents;

In XMLAppDelegate.h I use:

    @interface XMLAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;

    NSMutableArray *residents;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *residents;

@end

回答1:


You will need to use NSXMLParser (and NSXMLParserDelegate respectively) and correctly parse the XML data into the NSMutableArray before you can count anything. Also, to store everyday person's XML data, you may want to placing the information into NSMutableDictionaries.

In the .h @interface, you will need to create IVARs (example only):

 @interface YourObject : UIViewController <NSXMLParserDelegate> {
   NSXMLParser *parser;
   NSString *currentElement;
   NSMutableString *firstName;
   NSMutableString *lastName;
   NSMutableArray *residents;
 }

and somewhere in the .m the code, call this:

   NSString *xmlLocationString = @"http://www.website.com/feed.xml";
   NSURL *xmlLocationURL = [NSURL URLWithString:xmlLocationString];
   parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlLocationURL];
   [parser setDelegate:self];
   [parser setShouldProcessNamespaces:NO];
   [parser setShouldReportNamespacePrefixes:NO];
   [parser setShouldResolveExternalEntities:NO];
   [parser parse];

And of course, set up the delegate methods which will actually parse the XML document in to the NSMutableArray:

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

  currentElement = nil;
  currentElement = [elementName copy];
  if ([elementName isEqualToString:@"day"]) {
    firstName = [[NSMutableString alloc]init];
    lastName = [[NSMutableString alloc]init];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

   string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
   string = [string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];

   if ([currentElement isEqualToString:@"first"]) {
    [firstName appendString:string];
   } else if ([currentElement isEqualToString:@"last"]) {
    [lastName appendString:string];
   } else {
     ...
   }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{   
      if ([elementName isEqualToString:@"day"]) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        [dictionary setObject:firstName forKey:@"fname"];
        [dictionary setObject:lastName forKey:@"lname"];
        //And finally
        [residents addObject:dictionary];
      }
}

Then, anywhere in your code you can call

[residents count]; 

to get the total count for the residents.

Notice: I just wrote this code as I went, it probably has some bugs




回答2:


As of I understand you only need the count of . If so you can do following.

NSData *data = // your XML Data either from webservice or local file. Not xml string but the Data.
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];

This code will be where you get your data. In your .h file:

@interface yourViewController : UIViewController {
     NSInteger resedintsCount;
}

in your .m File:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
      if([elementName isEqualsToString:@"resident"]) {
           resedintsCount++;
      }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
     // you can use resedintsCount 
}

I assumed that you are getting your XML data in view controller. If you are getting it in AppDelegate or any other class than even the same process will be there. Still if you have in confusion feel free to ask.



来源:https://stackoverflow.com/questions/10337374/return-the-count-of-an-nsmutablearray-from-an-xml-id-number-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!