0

我已经查看了所有内容,包括此处和 Google,以找到一种为 iOS6 解析 XML 文件的简单方法。我发现的一切都与下一个略有不同,导致我极度困惑。到目前为止,这就是我所拥有的:

XMLParser.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class Profile;

@interface XMLParser : NSObject <NSXMLParserDelegate>{
    NSData *profileData;

@private
    Profile *currentProfileObject;

    NSMutableArray *currentParsedBatch;
    NSMutableArray *currentParsedCharacterData;

    BOOL accumlatingParsedCharacterData;
    BOOL didAbortParsing;

    NSUInteger parsedProfilesCounters;
}

@property (copy, readonly) NSData *profileData;

-(id)initWithData:(NSData *)parseData;

@end

我的 XMLParser.m

#import "XMLParser.h"
#import "Profile.h"

//Create a delegate
@interface XMLParser () <NSXMLParserDelegate>
    @property (nonatomic, retain) Profile *currentProfileObject;
    @property (nonatomic, retain) NSMutableArray *currentParsedBatch;
    @property (nonatomic, retain) NSMutableArray *currentParsedCharacterData;
@end

@implementation XMLParser
@synthesize profileData, currentProfileObject, currentParsedBatch, currentParsedCharacterData;

-(id)initWithData:(NSData *)parseData{
    if(self = [super init]){
        profileData = [parseData copy];
    }
    return self;
}

-(void)addProfilesToList:(NSArray *)profiles{
    assert([NSThread isMainThread]);

}

-(void)main{
    self.currentParsedBatch = [NSMutableArray array];
    self.currentParsedCharacterData = [NSMutableString string];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:profileData];

    [parser setDelegate:self];
    [parser parse];

    if([self.currentParsedBatch count] > 0){
        [self performSelectorOnMainThread:@selector(addProfilesToList:) withObject:self.currentParsedBatch waitUntilDone:NO];
    }

    self.currentParsedBatch = nil;
    self.currentProfileObject = nil;
    self.currentParsedCharacterData = nil;

    parser = nil;
}

/*
-(void)dealloc{
    [profileData release];
    [currentParsedCharacterData release];
    [currentParsedBatch release];

    [super dealloc];
}
*/

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

    NSLog(@"Parsing...");

    if([elementName isEqualToString:@"profiles"]){
        Profile *profile = [[Profile alloc] init];
        self.currentProfileObject = profile;
        //[profile release];
        profile = nil;
    }
    else if([elementName isEqualToString:@"profile"]){
        NSLog(@"Parsing....");

        [self.currentProfileObject setLastName:[attributeDict valueForKey:@"lastName"]];
        [self.currentProfileObject setFirstName:[attributeDict valueForKey:@"firstName"]];
        [self.currentProfileObject setMobile:[attributeDict valueForKey:@"mobile"]];
        [self.currentProfileObject setPhone:[attributeDict valueForKey:@"phone"]];
        [self.currentProfileObject setEmail:[attributeDict valueForKey:@"email"]];
        [self.currentProfileObject setAltEmail:[attributeDict valueForKey:@"altEmail"]];
        [self.currentProfileObject setAddress:[attributeDict valueForKey:@"address"]];
        [self.currentProfileObject setCity:[attributeDict valueForKey:@"city"]];
        [self.currentProfileObject setState:[attributeDict valueForKey:@"state"]];
        [self.currentProfileObject setZipCode:[attributeDict valueForKey:@"zipCode"]];
        [self.currentProfileObject setProfilePicture:[attributeDict valueForKey:@"profilePicture"]];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName{

    if([elementName isEqualToString:@"profile"]){
        [self.currentParsedBatch addObject:self.currentProfileObject];
        parsedProfilesCounters++;

    }
    else if([elementName isEqualToString:@"profiles"]){
        return;

    }
}

@end

现在在我的 appdelegate 文件中,我正在这样做:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    NSString *xmlFile = [[NSBundle mainBundle] pathForResource:@"profileXML" ofType:@"xml"];
    NSData *xmlData = [[NSData alloc] initWithContentsOfFile:xmlFile];

    [[XMLParser alloc] init];


    MediDirectoryMasterViewController *masterViewController = [[MediDirectoryMasterViewController alloc] initWithNibName:@"MediDirectoryMasterViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

这些似乎都不起作用,因为我没有从日志中得到任何响应。如果你不知道,这是我写过的第一个 iOS 应用程序,所以我不确定如何处理所有这些。再次,由于我开始失去信心,我们非常感谢您的帮助。

4

1 回答 1

2
  1. 如果您认为调用[[XMLParser alloc] init];会导致执行XMLParser main方法,那么您就错了。如果要调用该类的任何方法,则应该引用该类的实例,然后调用所需的方法。例如,在您的情况下:

    XMLParser *parser = [[XMLParser alloc] initWithData:xmlData];
    [parser main];
    [parser release];
    
  2. 您的代码中还有很多其他错误需要花费大量时间来讨论。这就是为什么我建议您阅读 Apple Docs 中的一些简单示例,即Your First iOS App。如果你不熟悉类和对象,你也应该阅读面向对象编程

于 2012-10-06T19:34:01.307 回答