【发布时间】:2013-12-27 12:39:37
【问题描述】:
我在网络上的某处有一个 XML 文件,其中包含几个配置文件,我希望在文本字段中显示配置文件的数量。
我有一个名为:numberOfProfiles..的文本字段。那么我应该在 viewDidLoad 中做什么?
numberOfProfiles.Text = ???
XML 文件被解析如下:
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:@"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);
[profile addObject:currentProfile];
}
if([elementName isEqualToString:@"lastname"])
{
currentProfile->lastName = currentNodeContent;
NSLog(@"%@",currentProfile->lastName);
[profile addObject:currentProfile];
}
if([elementName isEqualToString:@"email"])
{
currentProfile->eMail = currentNodeContent;
NSLog(@"%@",currentProfile->eMail);
[profile addObject:currentProfile];
}
if([elementName isEqualToString:@"address"])
{
currentProfile->address = currentNodeContent;
NSLog(@"%@",currentProfile->address);
[profile addObject:currentProfile];
}
if([elementName isEqualToString:@"profiles"])
{
[self->profile addObject:currentProfile];
currentProfile = nil;
currentNodeContent = nil;
setText:currentProfile->lastName;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
xmlParser = [[ViewController alloc] loadXMLByURL:@"http://dierenpensionlindehof.nl/profiles.xml"];
}
提前致谢!
【问题讨论】:
-
你试过这样:numberOfProfiles.Text = @"2" ?????
-
可以直接取profile数组count [profile count]
标签: ios objective-c xml parsing uitextfield