2

以下は、私が解析しようとしている基本的なセットアップを示すサンプル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;
    }
}

誰かが質問をつかむ方法を教えてもらえますか?

4

1 に答える 1

3

あなたは少し間違えました。'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]);
}

お役に立てば幸いです。

于 2011-09-13T10:01:01.927 に答える