2011-02-10 82 views
2

這裏是XML如何在iPhone中解析gdata xml?

Root---> 
    Subject---> 
      SubjectID 1 /SubjectID 
      SubjectName MatheMatics /SubjectName 
         Sub_Subject----> 
           Sub_SubjectID 1 /Sub_SubjectID 
           Sub_SubjectName Calculus /Sub_SubjectName 
         /Sub_Subject 
         Sub_Subject 
         Sub_SubjectID 2 /Sub_SubjectID 
         Sub_SubjectName Geometry /Sub_SubjectName 
         /Sub_Subject 
     /Subject 
     Subject----> 
      SubjectID 2 /SubjectID 
      SubjectName Physics /SubjectName 
     /Subject 
/Root 

的應用程序,我想在tableviewcontroller顯示主題名稱時,應用程序啓動&當我們點擊tableviewcell它會導致上顯示sub_subject列表中的另一表視圖。

如何實現這個 。

+ (NSString *)dataFilePath:(BOOL)forSave { 
    return [[NSBundle mainBundle] pathForResource:@"SubjectData" ofType:@"xml"]; 
} 

+ (RootSubject *)loadParty { 

    NSString *filePath = [self dataFilePath:FALSE]; 
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath]; 
    NSError *error; 
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData 
                  options:0 error:&error]; 
    /* if (doc == nil) { return nil; } 

    NSLog(@"%@", doc.rootElement); 

    [doc release]; 
    [xmlData release]; 
    return nil;*/ 
    RootSubject *rootSubject = [[[RootSubject alloc] init] autorelease]; 

    NSArray *partyMembers = [doc nodesForXPath:@"//Root/Subject" error:nil]; 
    //NSArray *patry = [doc nodesForXPath:@"//Root/Subject/SunjectID" error:nil]; 
    for (GDataXMLElement *partyMember in partyMembers) { 

     // Let's fill these in! 
     NSString *subjectId, *subjectName, *sub_SubjectId, *sub_SubjectName; 


     // senderName 
     NSArray *subjectIds = [partyMember elementsForName:@"SubjectID"]; 
     if (subjectIds.count > 0) { 
      GDataXMLElement *firstName = (GDataXMLElement *) [subjectIds objectAtIndex:0]; 
      subjectId = firstName.stringValue; 
     } else continue; 

     // senderEmail 
     NSArray *subjectNames = [partyMember elementsForName:@"SubjectName"]; 
     if (subjectNames.count > 0) { 
      GDataXMLElement *firstLevel = (GDataXMLElement *) [subjectNames objectAtIndex:0]; 
      subjectName = firstLevel.stringValue; 
     } else continue; 

    /* //senderPhone 
     NSArray *sub_SubjectIds = [partyMember elementsForName:@"Sub_SubjectID"]; 
     if (sub_SubjectIds.count > 0) { 
      GDataXMLElement *firstName = (GDataXMLElement *) [sub_SubjectIds objectAtIndex:0]; 
      sub_SubjectId = firstName.stringValue; 
     } else continue; 

     //senderLocation 
     NSArray *sub_SubjectNames = [partyMember elementsForName:@"Sub_SubjectName"]; 
     if (sub_SubjectNames.count > 0) { 
      GDataXMLElement *firstName = (GDataXMLElement *) [sub_SubjectNames objectAtIndex:0]; 
      sub_SubjectName = firstName.stringValue; 
     } else continue;*/ 

     //Subject *subject = [[[Subject alloc]initWithSubjectId:subjectId subjectName:subjectName sub_SubjectId:sub_SubjectId sub_SubjectName:sub_SubjectName]autorelease]; 
     Subject *subject = [[[Subject alloc]initWithSubjectId:subjectId subjectName:subjectName sub_SubjectId:nil sub_SubjectName:nil]autorelease]; 
     //Subject *subject = [[[Subject alloc]initWithSubjectId:subjectId subjectName:nil sub_SubjectId:sub_SubjectId sub_SubjectName:sub_SubjectName]autorelease]; 

     //[rootGroup.groups addObject:group]; 
     [rootSubject.subjects addObject:subject]; 

    } 
    [doc release]; 
    [xmlData release]; 
    return rootSubject; 



} 

通過這個我們獲得了主體名稱和tableviewcontroller完美地表現出來,但是當我在它提供了另一個視圖控制器

+0

兩件事情:1:請學會正確格式化您的答案。如果您選擇了一個代碼片段,並且他們按下帶有「{}」圖標的按鈕,它將會正確格式化。 2:看起來您在按提交之前沒有完成問題的撰寫。 – JeremyP 2011-02-10 09:05:44

回答