作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我查看了所有内容,包括此处和 Google,以找到一种为 iOS6 解析 XML 文件的简单方法。我发现的所有内容都与下一个略有不同,这让我非常困惑。到目前为止,这就是我所拥有的:
XML解析器.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 应用程序,所以我不确定如何处理所有这些。再次,非常感谢您的帮助,因为我开始失去信心。
最佳答案
如果您认为调用 [[XMLParser alloc] init];
会导致执行 XMLParser
main
方法,那么您是错误的。如果您想调用该类的任何方法,您应该引用该类的实例,然后调用所需的方法。例如,在您的情况下:
XMLParser *parser = [[XMLParser alloc] initWithData:xmlData];
[parser main];
[parser release];
您的代码中还有很多其他错误,需要花很多时间来讨论。这就是为什么我建议您阅读 Apple Docs 中的一些简单示例,即 Your First iOS App .如果您不熟悉类和对象,您应该阅读 object-oriented programming也是。
关于objective-c - 解析 XML 文件 - Losing My Mind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12760439/
我是一名优秀的程序员,十分优秀!