2011-09-13 3 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 : @ "questions"를 호출합니다. "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]); 
} 

내가 당신을 도와 바랍니다.

관련 문제