简体   繁体   English

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

[英]How to display XML parsed data in UILabel

I am trying to display the data parsed from an XML file from the web to a UILabel called profilesLabel. 我正在尝试显示从Web的XML文件解析的数据到名为profilesLabel的UILabel。

I'm trying to display all the lastName's in the label, but the value is set to the email address for some reason. 我试图显示标签中的所有姓氏,但由于某种原因,该值设置为电子邮件地址。 Can someone help me understand why as I'm new to objective-c 有人可以帮助我了解我为什么不熟悉Objective-C吗

here is the code: 这是代码:

-(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"];

}

Any help would be appreciated, Tahnks! 任何帮助将不胜感激,Tahnks!

is the issue that here: 这里的问题是:

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

you are setting currentProfile to nil 您正在将currentProfile设置为nil

and then using currentProfile here: 然后在这里使用currentProfile:

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

EDIT: After clarification that the issue was wanting to know how to display different data. 编辑:澄清该问题后想知道如何显示不同的数据。

-(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];
}

EDIT 2: 编辑2:

After yet more clarification that the issue is that you don't have the last names, please draw your attention to the fact that you are assigning everything to lastName in this function: 在进一步说明问题是您没有姓氏之后,请引起注意以下事实:您正在此函数中将所有内容分配给lastName:

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;
}

in every section you use: currentProfile->lastName = currentNodeContent; 在您使用的每个部分中: currentProfile->lastName = currentNodeContent; you need to set the other values inside currentProfile !!! 您需要在currentProfile中设置其他值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM