2011-09-13 10 views
2

以下は、解析しようとしている基本設定を示すサンプルXMLです。ノード内のノードにアクセスする方法GDataXML

これまでのところ、タスク、タスク、ヒント、エクササイズ、テキストのデータを簡単に抽出することができ、エクササイズ時にtypeという属性を取得することもできます。

しかし私は私の人生のために、タグの質問を含む質問ブロックを取得する方法を理解できません。

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

誰もが疑問をつかむためにどのようにうまくいけば教えていただけます:ここで

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

は、私がこれまでのデータを取得する方法は?

+0

何らかの理由で私のサンプルxmlが最初の数行を削除しているように見えることはありませんか? – user7865437

答えて

3

あなたは少し間違いをしました。エクササイズルートからではなく、タスクルートから 'elementsForName:@ "question"を呼び出します。 「質問」要素はタスク要素には存在せず、運動要素にのみ存在するため、機能しません。

ソリューションは、次のようになります。

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

私はそれはあなたを助けるいただければ幸いです。

関連する問題