问题
I have 2 classes: XMLParser
and ViewController
. All the code for the parsing process is in the XMLParser
. I call the date being parsed in ViewController
and display it in Command Output window using NSLog
. Now my problem is I can't seem to get the data to be displayed in a UILabel
called "profilesLabel". In the ViewController
is a UIButton
called "showProfilesButton" which should display the data in profilesLabels text. See the attempted code in ViewController.m.
Any help would be appreciated!
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
来源:https://stackoverflow.com/questions/20849144/how-to-display-xml-parsed-data-in-a-label-ios