我已经查看了所有内容,包括此处和 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 应用程序,所以我不确定如何处理所有这些。再次,由于我开始失去信心,我们非常感谢您的帮助。