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

时间:2013-12-23 14:48:34

标签: ios objective-c xml xcode uilabel

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

我正在尝试显示标签中的所有lastName,但由于某种原因,该值设置为电子邮件地址。有人可以帮助我理解为什么我是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!

1 个答案:

答案 0 :(得分:1)

是这里的问题:

    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:

在进一步澄清问题是您没有姓氏之后,请提请您注意以下事实:您在此函数中将所有内容分配给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;
}

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