解析XML文件 - 失去理智

时间:2012-10-06 13:59:23

标签: objective-c ios ios6 nsxmlparser

我看了很多,包括这里和谷歌找到一种简单的方法来解析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应用程序,所以我不知道如何解决所有这些问题。再次,当我开始失去信心时,非常感谢帮助。

1 个答案:

答案 0 :(得分:2)

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

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