我能够解析xml数据并能够在控制台中显示它,但无法在UITextField
或UILabel
中显示该数据。
我也尝试使用viewDidLoad
方法分配到textfield。
以下是我的代码,
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"];
}
答案 0 :(得分:1)
不是将值直接分配给'UITextField'或'UILabel',而是将值存储在字符串中。解析操作完成后,将字符串值赋给'UITextField'或'UILabel';你应该在'-viewWillAppear'方法中做到这一点: - )
答案 1 :(得分:0)
创建IBOutlet UILabel *标签;在界面中,在Interface Builder中连接它,然后你可以通过label.text = @“Anything”设置它的文本;
答案 2 :(得分:0)
解析XML解析器时我也遇到了同样的问题。
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
您正在使用上面的行修剪空格和制表符。但有时它不起作用。
所以,我实现了以下代码。代码可能对您有用。
NSString *sname = [currentNodeContent stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *actualString = [sname stringByReplacingOccurrencesOfString:@"\t" withString:@""];
然后将actualString
传递给您需要的任何内容。