gpt4 book ai didi

ios - 如何在 UILabel 中显示 XML 解析后的数据

转载 作者:行者123 更新时间:2023-11-29 03:23:49 25 4
gpt4 key购买 nike

我正在尝试将从 Web 的 XML 文件解析的数据显示到名为 profilesLabel 的 UILabel。

我试图在标签中显示所有姓氏,但由于某种原因该值被设置为电子邮件地址。有人可以帮助我理解为什么我是 objective-c 的新手

代码如下:

-(id)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];
return self;
}

-(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 = [ViewController alloc];
isStatus = YES;
}

if([elementName isEqualToString:@"lastname"])
{
currentProfile = [ViewController alloc];
isStatus = YES;
}

if([elementName isEqualToString:@"email"])
{
currentProfile = [ViewController alloc];
isStatus = YES;
}

if([elementName isEqualToString:@"address"])
{
currentProfile = [ViewController 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);
}

if([elementName isEqualToString:@"lastname"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
last_Name.text = currentProfile->lastName;
}

if([elementName isEqualToString:@"email"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
last_Name.text = currentProfile->lastName;
}

if([elementName isEqualToString:@"address"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
last_Name.text = currentProfile->lastName;
}


if([elementName isEqualToString:@"profiles"])
{
[self->profile addObject:currentProfile];
currentProfile = nil;
currentNodeContent = nil;
}
}


- (IBAction)bringFont:(id)sender
{
//Here is a button that should show the parsed data in profilesLabel when pressed
profilesLabel.text = currentProfile->lastName; //Shows email address for some reason
//What I want is to show every single profile in profilesLabel underneath each other
}

- (void)viewDidLoad
{

[profilesScrolView setScrollEnabled:YES];
[profilesScrolView setContentSize:CGSizeMake(680, 1200)];


[super viewDidLoad];
xmlParser = [[ViewController alloc] loadXMLByURL:@"http://dierenpensionlindehof.nl/XML_File.xml"];

}

任何帮助,Tahnks,我们将不胜感激!

最佳答案

这里的问题是:

    if([elementName isEqualToString:@"profiles"])
{
[self->profile addObject:currentProfile];
currentProfile = nil;
currentNodeContent = nil;
}

您正在将 currentProfile 设置为 nil

然后在这里使用 currentProfile:

- (IBAction)bringFont:(id)sender
{
//Here is a button that should show the parsed data in profilesLabel when pressed
profilesLabel.text = currentProfile->lastName;

编辑:经过澄清问题是想知道如何显示不同的数据。

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

...

// At the end of each loop add current profile to the mutable array
[profile addObject:currentProfile];

// Now you can set currentProfile to nil
currentProfile = nil;
}


- (IBAction)bringFont:(id)sender
{
// If you want to display all of this data I would recommend creating a
// UITableView and have each entry as a row's title. But thats another
// issue so to set them all into the label as you asked:

// Loop through the array and add the strings together adding a newline each time
NSMutableString *str = [[NSMutableString alloc] init];

for(NSObject *obj in profile)
{
// Obj will need to be casted to whatever type 'currentProfile' is
[str appendFormat:@"\n %@", obj->lastName];
}

// Compute what height the UILabel needs to be to display this string
CGSize expectedSize = [str sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];

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

编辑 2:

在进一步澄清问题是您没有姓氏之后,请注意您在此函数中将所有内容都分配给了姓氏:

if([elementName isEqualToString:@"lastname"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
last_Name.text = currentProfile->lastName;
}

if([elementName isEqualToString:@"email"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
last_Name.text = currentProfile->lastName;
}

if([elementName isEqualToString:@"address"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
last_Name.text = currentProfile->lastName;
}

在您使用的每个部分中:currentProfile->lastName = currentNodeContent;您需要在 currentProfile 中设置其他值!!!

关于ios - 如何在 UILabel 中显示 XML 解析后的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com