我有两个课程:XMLParser
和ViewController
。解析过程的所有代码都在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