user7865437
user7865437

Reputation: 812

How to access nodes within nodes GDataXML

Below is some sample XML showing the basic setup I am trying to parse.

So far I can easily extract the data for tasks, task, title, hint, exercise and text as well as grabbing the attribute type in exercise.

However I cannot for the life of me figure out how to get the questions block that includes the tag question.

<?xml version="1.0" encoding="UTF-8" ?>
<tasks>
    <task>
    <title>Any ole text goes here</title>
    <hint>dont cross busy roads!</hint>
    <exercise type="yes_no">
            <text>which planet is nearest the sun?</text>
            <questions>
                    <question answer="false">Mars</question>
                    <question answer="true">Mercury</question>
                    <question answer="false">Saturn</question>
            </questions>
    </exercise>
</tasks>

Here's how I get the data thus far:

-(void)createTask
{   
    self.task = [[Task alloc] init];

    // grab the task from the loaded xml
    NSArray *tasks = [[AppData sharedInstance].XMLTaskDocument.rootElement elementsForName:@"task"];

    // cycle through the task and extract its data assigning to appropriate model property
    for (GDataXMLElement *task in tasks )
    {   
        NSString *title = nil;
        NSArray *titles = [task elementsForName:@"title"];

        if ([titles count] > 0)
        {   
            GDataXMLElement *firstTitle = (GDataXMLElement *)[titles objectAtIndex:0];      
            title = firstTitle.stringValue; 
        } else continue;

        NSString *hint = nil;
        NSArray *hints = [task elementsForName:@"hint"];

        if ([hints count] > 0)
        {
            GDataXMLElement *firstHint = (GDataXMLElement *)[hints objectAtIndex:0];
            hint = firstHint.stringValue;   
        } else continue;


        NSString *type = nil;
        NSString *text = nil;
        NSArray *exercises = [task elementsForName:@"exercise"];

        if ([exercises count] > 0)
        {
            type = [(GDataXMLNode *)[[exercises objectAtIndex:0] attributeForName:@"type"] stringValue];

            GDataXMLElement *firstText = (GDataXMLElement *)[exercises objectAtIndex:0];
            text = firstText.stringValue;

            // THIS DOES NOT WORK :-(       
            NSArray *questions = [task elementsForName:@"questions"];
            if ([questions count] > 0)
            {
                NSLog(@"questions count is: %d", [questions count]);
            }   
        } else continue;
    }
}

Can anyone tell me hopefully how to grab questions?

Upvotes: 2

Views: 1903

Answers (1)

Yannick Loriot
Yannick Loriot

Reputation: 7136

You have made a little mistake. You call the 'elementsForName:@"questions"' from the task root and not from the exercise root. It doesn't work because the "question" element doesn't exist into the task element but only into the exercise element.

The solution should look like that:

// Replace this
NSArray *questions = [task elementsForName:@"questions"];
if ([questions count] > 0)
{
        NSLog(@"questions count is: %d", [questions count]);
}

// By this
NSArray *questions = [[exercises objectAtIndex:0] elementsForName:@"questions"];
if ([questions count] > 0)
{
        NSLog(@"questions count is: %d", [questions count]);
}

I hope it'll help you.

Upvotes: 3

Related Questions