Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6143875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:34:40+00:00 2026-05-23T18:34:40+00:00

Maybe someone can help … I have a problem with the fact that during

  • 0

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

Screenshot MainWindow.xib

Please tell me where I could be wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:34:41+00:00Added an answer on May 23, 2026 at 6:34 pm

    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.

    - (void)viewDidLoad
    {    
        [super viewDidLoad];
    
        if (nil == xmlcont) {
            xmlcont = [[XMLController alloc] loadXMLByURL:@"http://link_to_XML_file.xml"];
            NSLog(@"array = %@", [xmlcont newsArray]);
    
            for (myNews *oneNew in [xmlcont newsArray]) {
                NSLog(@"URL = %@", [oneNew itemImageUrl]);
            }
        }
    }
    

    PS If you add this code and still get the output twice then you’re making two versions of the ViewController.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with toggling and document functions, maybe someone can help me
Hi maybe someone can help me here ... I have a slight problem with
I have the following problem, maybe someone can help me (or explain, where my
I have one problem with a gridview control and maybe someone can help me.
I have a question related with JPopupMenu that maybe someone can help me. As
Okay, the specs have changed on this one somewhat. Maybe someone can help me
Maybe someone can help me out. I have created a simple web user control,
A have a question, maybe someone can help me. I am trying to make
I am hoping that someone can help me with this relative simple problem: <?php
I'm finding this a bit tricky! Maybe someone can help me on this one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.