如何在标签iOS中显示XML解析数据

时间:2013-12-30 23:11:28

标签: ios objective-c xml uilabel

我有两个课程:XMLParserViewController。解析过程的所有代码都在XMLParser中。我在ViewController中调用正在解析的日期,并使用NSLog在命令输出窗口中显示它。现在我的问题是我似乎无法将数据显示在名为“profilesLabel”的UILabel中。在ViewController中有一个名为“showProfilesButton”的UIButton,它应该在profilesLabels文本中显示数据。请参阅ViewController.m中的尝试代码。

任何帮助将不胜感激!

XMLParser.h

#import <Foundation/Foundation.h>
#import "ViewController.h"

@interface XMLParser : NSObject

{
    bool isStatus;
    XMLParser *currentProfile;
    XMLParser *xmlParser;
    NSXMLParser *parser;
    NSMutableString *currentNodeContent;
    NSMutableArray *profile;
    NSString *firstName;
}

- (void)loadXMLByURL:(NSString *)urlString;
- (void)loadXML;

@end

XMLParser.m

#import "XMLParser.h"

@implementation XMLParser

-(void)loadXMLByURL:(NSString *)urlString
{
    profile = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile = [XMLParser alloc];
        isStatus = YES;
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile->firstName = currentNodeContent;
        NSLog(@"%@",currentProfile->firstName);
        [profile addObject:currentProfile];
    }
}

-(void)loadXML
{
    [self loadXMLByURL:@"http://dierenpensionlindehof.nl/profiles1.xml"];
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "XMLParser.h"

@interface ViewController : UIViewController
{

}

@property (weak, nonatomic) IBOutlet UILabel *profilesLabel;

@end

ViewController.m

#import "ViewController.h"
#import "XMLParser.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize profilesLabel;



//Code for the button to display the profiles
- (IBAction)showProfilesButton:(id)sender
{
    XMLParser* parser = [[XMLParser alloc]init];
    [parser loadXML];


    NSMutableString *str = [[NSMutableString alloc] init];

    for(XMLParser *obj in self.profile)
    {
        [str appendFormat:@"\n %@", obj->firstName];
        profilesLabel.text = str;
    }

    CGSize expectedSize = [str sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(520, 1000) lineBreakMode:NSLineBreakByWordWrapping];

    CGRect newFrame = CGRectMake(0, 0, expectedSize.width, expectedSize.height);
    [profilesLabel setFrame:newFrame];
    [profilesLabel setText:str];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

0 个答案:

没有答案