Maybe someone can help …
I have a problem with the fact that during the parsing of the XML file we have two arrays. Such is the construction of TWO identical returns an array:
xmlcont = [[XMLController alloc] loadXMLByURL:@"http://link_to_XML_file.xml"];
I have the following files:
Constants.h
#import <Foundation/Foundation.h>
extern NSString * const ITEM;
extern NSString * const TITLE;
extern NSString * const IMAGE;
extern NSString * const DESCRIPTION;
extern NSString * const TEXT;
@interface Constants : NSObject
{
}
@end
Constants.m
#import "Constants.h"
@implementation Constants
NSString * const ITEM = @"item";
NSString * const TITLE = @"title";
NSString * const IMAGE = @"image";
NSString * const DESCRIPTION = @"description";
NSString * const TEXT = @"text";
- (void)dealloc
{
[super dealloc];
}
@end
myNews.h
#import <Foundation/Foundation.h>
@interface myNews : NSObject
{
NSString *itemTitle;
NSString *itemImageUrl;
NSString *itemDescription;
NSString *itemText;
}
@property (nonatomic, retain) NSString *itemTitle;
@property (nonatomic, retain) NSString *itemImageUrl;
@property (nonatomic, retain) NSString *itemDescription;
@property (nonatomic, retain) NSString *itemText;
@end
myNews.m
#import "myNews.h"
@implementation myNews
@synthesize itemTitle;
@synthesize itemImageUrl;
@synthesize itemDescription;
@synthesize itemText;
@end
XMLController.h
#import <Foundation/Foundation.h>
@class Constants;
@class myNews;
@interface XMLController : NSObject
{
NSMutableString *currentNodeContent;
NSMutableArray *newsArray;
NSXMLParser *parser;
myNews *currentNew;
}
@property (readonly, retain) NSMutableArray *newsArray;
-(id)loadXMLByURL:(NSString *)urlString;
@end
XMLController.m
#import "XMLController.h"
#import "Constants.h"
#import "myNews.h"
@implementation XMLController
@synthesize newsArray;
-(id)loadXMLByURL:(NSString *)urlString
{
newsArray = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString: urlString];
parser = [[NSXMLParser alloc] initWithContentsOfURL: url];
[parser setDelegate:(id)self];
[parser parse];
return self;
}
-(void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString: ITEM])
{
currentNew = [myNews alloc];
currentNodeContent = [[NSMutableArray alloc] init];
}
}
-(void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString: TITLE])
{
currentNew.itemTitle = currentNodeContent;
}
if ([elementName isEqualToString: IMAGE])
{
currentNew.itemImageUrl = currentNodeContent;
}
if ([elementName isEqualToString: DESCRIPTION])
{
currentNew.itemDescription = currentNodeContent;
}
if ([elementName isEqualToString: TEXT])
{
currentNew.itemText = currentNodeContent;
}
if ([elementName isEqualToString: ITEM])
{
[newsArray addObject:currentNew];
[currentNew release];
currentNew = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
-(void) parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
currentNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
@end
myAppDelegate.h
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#import "DetailViewController.h"
@class MainViewController;
@class DetailViewController;
@interface myAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *myNavigationController;
MainViewController *myMainViewController;
DetailViewController *myDetailViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *myNavigationController;
@property (nonatomic, retain) IBOutlet MainViewController *myMainViewController;
@property (nonatomic, retain) IBOutlet DetailViewController *myDetailViewController;
@end
myAppDelegate.m
#import "myAppDelegate.h"
#import "myNews.h"
#import "MainViewController.h"
#import "DetailViewController.h"
@implementation myAppDelegate
@synthesize window;
@synthesize myNavigationController;
@synthesize myMainViewController;
@synthesize myDetailViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[window addSubview:myNavigationController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[window release];
[myNavigationController release];
[myMainViewController release];
[myDetailViewController release];
[super dealloc];
}
@end
MainViewController.h
#import <UIKit/UIKit.h>
#import "XMLController.h"
@interface MainViewController : UIViewController
{
XMLController *xmlcont;
}
@end
MainViewController.m
#import "MainViewController.h"
#import "myNews.h"
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
xmlcont = [[XMLController alloc] loadXMLByURL:@"http://link_to_XML_file.xml"];
NSLog(@"array = %@", [xmlcont newsArray]);
for (myNews *oneNew in [xmlcont newsArray]) {
NSLog(@"URL = %@", [oneNew itemImageUrl]);
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc
{
[super dealloc];
}
@end
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<channel>
<item>
<title>news 1</title>
<image>http://link_to_JPG_file_1.jpg</image>
<description>description 1</description>
<text>text 1</text>
</item>
<item>
<title>news 2</title>
<image>http://link_to_JPG_file_2.jpg</image>
<description>description 2</description>
<text>text 2</text>
</item>
</channel>
As a result, the implementation of the output I see:
2011-07-14 13:18:27.785 my[18673:207] array = (
"<myNews: 0x4e28dc0>",
"<myNews: 0x4e29080>"
)
2011-07-14 13:18:27.787 my[18673:207] URL = http://link_to_JPG_file_1.jpg
2011-07-14 13:18:27.818 my[18673:207] URL = http://link_to_JPG_file_2.jpg
2011-07-14 13:18:27.959 my[18673:207] array = (
"<myNews: 0x4b5a010>",
"<myNews: 0x4b5a310>"
)
2011-07-14 13:18:27.960 my[18673:207] URL = http://link_to_JPG_file_1.jpg
2011-07-14 13:18:27.963 my[18673:207] URL = http://link_to_JPG_file_2.jpg
MainWindow.xib
Please tell me where I could be wrong?
You’re getting viewDidLoad called twice (which isn’t necessarily a bug). You just need to add some logic to only load your XML once.
PS If you add this code and still get the output twice then you’re making two versions of the ViewController.