0

我能够解析 xml 数据并能够在控制台中显示它,但无法UITextFieldUILabel.

我也尝试在viewDidLoad方法中分配给文本字段。

以下是我的代码,

NSMutableString *currentNodeContent;
NSXMLParser *parser;
ViewController *currentProfile;
bool isStatus;

ViewController *xmlParser;

-(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:@"profileinfo"])
    {
        currentProfile = [ViewController alloc];
        isStatus = YES;
    }

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

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"first_name"])
    {
        currentProfile.firstName = currentNodeContent;
        NSLog(@"%@",currentProfile.firstName);
        first_Name.text = currentNodeContent;//UITextField
        first_name.text = currentNodeContent;//Label

    }
    if([elementName isEqualToString:@"last_name"])
    {
        currentProfile.lastName = currentNodeContent;
         NSLog(@"%@",currentProfile.lastName);
        last_Name.text = currentProfile.lastName;
    }
    if([elementName isEqualToString:@"profileinfo"])
    {
        [self.profile addObject:currentProfile];
        currentProfile = nil;
        currentNodeContent = nil;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    xmlParser = [[ViewController alloc] loadXMLByURL:@"http://www.mxxxxx.net/xxx/xxxxx.aspx?type=proifileinfo&loginid=xxx@gmail.com"];

}
4

3 回答 3

1

不是将值直接分配给“UITextField”或“UILabel”,而是将值存储在字符串中。并且当解析动作完成后,将字符串值赋给'UITextField'或'UILabel';可能你应该在'-viewWillAppear'方法中这样做:-)

于 2012-07-20T10:44:38.763 回答
0

我在解析 XML 解析器时也遇到了同样的问题。

currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

您正在使用上面的行来修剪空格和制表符。但有时它不起作用。

所以,我实现了以下代码。该代码可能对您有用。

NSString *sname = [currentNodeContent stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSString *actualString = [sname stringByReplacingOccurrencesOfString:@"\t" withString:@""];

然后传递actualString给你需要的任何东西。

于 2012-07-20T09:52:42.533 回答
0

创建一个 IBOutlet UILabel *label; 在界面中,在Interface Builder中连接,然后可以通过label.text = @"Anything";

于 2012-07-20T08:56:53.557 回答