iOS6用のXMLファイルを解析する簡単な方法を見つけるために、こことGoogleを含め、すべてを見てきました。私が見つけたものはすべて次のものとは少し異なり、私の側で極端な混乱を引き起こしています。これまでのところ、これは私が持っているものです:
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アプリであるため、これらすべてを実行する方法がわかりません。繰り返しになりますが、私は信仰を失い始めているので、助けていただければ幸いです。