如何访问节点 GDataXML 中的节点

发布于 2024-12-04 07:18:00 字数 2490 浏览 1 评论 0原文

下面是一些示例 XML,显示了我尝试解析的基本设置。

到目前为止,我可以轻松提取任务、任务、标题、提示、练习和文本的数据,并获取练习中的属性type

然而,我一生都无法弄清楚如何获取包含标签问题的问题块。

<?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>

到目前为止,我是这样获取数据的:

-(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;
    }
}

谁能告诉我如何抓住问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-12-11 07:18:00

你犯了一个小错误。您从任务根而不是练习根中调用“elementsForName:@"questions"”。它不起作用,因为“问题”元素不存在于任务元素中,而仅存在于练习元素中。

解决方案应该如下所示:

// 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]);
}

我希望它能帮助您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文