Hi. I use an instance of NSXMLParser. I store found chars in NSMutableStrings that are stored in an NSMutableDictionary and these Dicts are then added to an NSMutableArray.
When I test this everything seems normal: I count 1 array, x dictionnaries and x strings.
In a detailview controller file I want to show my parsed results. I call the class where everthing is stored but I get (null) returned.
This is what I do (wrong):
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];
}
In my controller file I do this:
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);
}
When I test the value of my array, a dict or an element in the XMLParser.h file I get my result. What am I doing wrong so I can't call my results in my controller file? Any help is welcome, because I'm pretty stuck right now :/