J'utilise une instance de NSXMLParser. Je stocke les caractères trouvés dans NSMutableStrings qui sont stockés dans un NSMutableDictionary et ces Dicts sont ensuite ajoutés à un NSMutableArray.Passant la valeur NULL
Quand je teste cela, tout semble normal: je compte 1 tableau, x dictionnaires et x chaînes.
Dans un fichier de contrôleur de détail, je souhaite afficher mes résultats analysés. J'appelle la classe où tout est stocké mais je reçois (null) retourné.
C'est ce que je fais (mal):
xAppDelegate.h
@interface xAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
xAppDelegate.m
#import "xAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"
@implementation xAppDelegate
@synthesize window, navigationController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// OFFLINE DOCUMENT > Resources folder
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"file.xml"];
NSData *Data = [[NSData alloc] initWithContentsOfFile:DataPath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:Data];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
[window addSubview: navigationController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[navigationController release];
[super dealloc];
}
@end
XMLParser.h
@class xAppDelegate;
@interface XMLParser : NSObject {
NSMutableArray *array;
NSMUtableDictionary *dictionary;
NSSMutabletring *element;
xAppDelegate *appDelegate;
}
- (XMLParser *) initXMLParser;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableDictionary *dictionary;
@property (nonatomic, retain) NSMutableString *element;
XMLParser.m
#import "xAppDelegate.h"
#import "XMLParser.h"
@synthesize array, dictionary, element;
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (xAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
array = [[NSMutableArray alloc] init];
dictionary = [[NSMutableDictionary alloc] init];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
ele = [elementName copy];
if ([elementName isEqualToString:@"CONTAINER"]) {
element = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSMutableString *)string
{
if ([ele isEqualToString:@"ELEMENTNAME"]) {
[element appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"CONTAINER"]) {
[dictionary setObject:element forKey:@"ELEMENTNAME"];
[array addObject:[dictionary copy]];
}
}
- (void) dealloc {
[element release];
[dictionary release];
[array release];
}
Dans mon fichier du contrôleur que je fais ceci:
controller.h
@class XMLParser;
@interface controller : UIViewController {
XMLParser *aXMLParser;
}
@property (nonatomic, retain) XMLParser *aXMLParser;
controller.m
#import "XMLParser.h"
@synthesize aXMLParser;
- (void)viewDidLoad {
NSLog(@"test array: %@", aXMLParser.array);
NSLog(@"test dict: %@", aXMLParser.dictionary);
NSLog(@"test element: %@", aXMLParser.element);
}
Lorsque je teste la valeur de mon tableau, un dict ou un élément le fichier XMLParser.h j'obtiens mon résultat. Qu'est-ce que je fais de mal donc je ne peux pas appeler mes résultats dans mon fichier de contrôleur? Toute aide est la bienvenue, car je suis bloqué en ce moment:/
Ceci est Objective-C. – BobbyShaftoe
Merci de remarquer que BobbyShaftoe. – FFXIII