开发者

how to parse gdata xml in iPhone?

开发者 https://www.devze.com 2023-02-09 10:43 出处:网络
here is xml Root---> Subject---> SubjectID 1/SubjectID SubjectName MatheMatics /SubjectName Sub_Subject---->

here is 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 

in app i want to show subject name in tableviewcontroller when application launch & when we clicked on tableviewcell it leads on another table view which shows sub_subject list.

how to achieve this for that.

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



}

through this we get subject name and show it perfectly in tableviewcontroller but when i clicked on particu开发者_如何学Golar subject which provides subsubject in another view controller


Here is a good tutorial that shows XML parsing using GDataXMLParser.

how-to-read-and-write-xml-documents-with-gdataxml

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号