NSXMLParser:解析XML文件显示两个相同的数组

时间:2011-07-14 08:34:17

标签: iphone xml arrays parsing

也许有人可以提供帮助......

我遇到的问题是,在解析XML文件时,我们有两个数组。这就是构造两个相同的返回数组:

xmlcont = [[XMLController alloc] loadXMLByURL:@"http://link_to_XML_file.xml"];

我有以下文件:

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文件:

<?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>

结果,我看到输出的实现:

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

请告诉我哪里可能出错?

1 个答案:

答案 0 :(得分:0)

你正在调用viewDidLoad两次(这不一定是个bug)。您只需添加一些逻辑来仅加载一次XML。

- (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如果您添加此代码并仍然获得两次输出,那么您将制作两个版本的ViewController。