简体   繁体   中英

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.

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

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!

is the issue that here:

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

you are setting currentProfile to nil

and then using currentProfile here:

- (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:

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:

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; you need to set the other values inside currentProfile !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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