显示文本字段中XML元素的数量

时间:2013-12-27 12:39:37

标签: ios objective-c xml parsing uitextfield

我在网络上的某处有一个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"];

    }

提前致谢!

2 个答案:

答案 0 :(得分:2)

对于元素的计数,你可以有一个像int类型numberOfProfilesCount的实例变量,你可以在

中增加它
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
     numberOfProfilesCount++;
}

并在

- (void)parserDidEndDocument:(NSXMLParser *)parser {
   numberOfProfiles.Text = [NSString stringWithFormate:@"%d", numberOfProfilesCount];
}

显示您的元素数

答案 1 :(得分:1)

假设profile是全局变量,请使用[profile count]获取配置文件的数量。

尝试:[numberOfProfiles setText:[[profile count] stringValue]