Search code examples
iphoneiosxmlios7nsxmlparser

Fetching child tag from multi level xml in Objective C


My XML

<MemberBookDTO>
   <BookKey>abc</BookKey>
   <OrderLineKey>5f8-</OrderLineKey>
   <ValidationHash>512D0</ValidationHash>
   <BookId>101</BookId>
   <Name>Dönüş</Name>
   <Author>Abc</Author>
   <Narrator>Apn</Narrator>
   <OrderDateAsText>01.01.2011 15:00</OrderDateAsText>
   <Description>abcd123456.</Description>
   <TotalDurationAsSec>0</TotalDurationAsSec>
   <TotalSizeAsByte>658943266</TotalSizeAsByte>
   <ListImageURL>http://abc.com/SeslenenKitap/File/Picture/abc.png</ListImageURL>
   <DetailImageURL>http://abc.com/SKitap/File/Picture/abc.png</DetailImageURL>
   <PlayerImageURL>http://abc.com/SKitap/File/Picture/abc.png</PlayerImageURL>
   <ImageSign>63514212345693424</ImageSign>
   <Chapters>
      <ChapterDTO>
         <Id>350</Id>
         <Name>Bolum 1 - Kaybolmak</Name>
         <Sort>1</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/61efdf42-ce30-4c52-aa4f-84748fa28343.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>36</Id>
         <Name>Bolum 2 - Kacis</Name>
         <Sort>2</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://abc.com/Kitap/File/Audio/730a25a9-a01f-41ee-acda-3c23edbe078a.mp3</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>37</Id>
         <Name>Bölüm 3 - Karşılaşma</Name>
         <Sort>3</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/7d029835-99a6-4c24-b9f8-735d1496be2e.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>38</Id>
         <Name>Bolum 4 - Kavusma</Name>
         <Sort>4</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/f9e7535f-5ffb-4be8-ad00-67be3906bffe.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>39</Id>
         <Name>Bolum 5 - Yuzlesme</Name>
         <Sort>5</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/ae4cdab9-7fdc-4c9b-b2b7-cec7299d2353.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>40</Id>
         <Name>Bolum 6 - Itiraf</Name>
         <Sort>6</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/60bfc2f9-9fe0-4c90-bdd0-c6253bca7056.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>41</Id>
         <Name>Bolum 7 - Donus</Name>
         <Sort>7</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/45b31c6a-9b38-4b00-91f1-4d9153dff138.mp3</DownloadURL>
      </ChapterDTO>
   </Chapters>
</MemberBookDTO>

Objective C Code:

 -(id) loadXMLByURL:(NSString *)urlString
{
    bookListArray= [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    parser= [[NSXMLParser alloc] initWithContentsOfURL:url];
    parser.delegate=self;
    [parser parse];
    return self;
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"MemberBookDTO"])
    {
        currentBook = [[BookListInfo alloc] init];
        currentNodeContentBook=[[NSMutableString alloc] init];
    }

    else if ([elementname isEqualToString:@"Chapters"])
    {
        //currentBook = [[BookListInfo alloc] init];
        currentNodeContentChapters=[[NSMutableString alloc] init];
    }


}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
  //  bookKey,orderLineKey,bookAuthor,bookDescription,bookDuration,bookId,bookName,bookNarrator,bookOrderDate,bookSizeAtByte,listImageUrl,detailImageUrl,playerImageUrl,imageSign;
    if ([elementname isEqualToString:@"BookKey"])
    {
        currentBook.bookKey = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"OrderLineKey"])
    {
        currentBook.orderLineKey = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"ValidationHash"])
    {
        currentBook.validationKey = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"BookId"])
    {
        currentBook.bookId = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Name"])
    {
        currentBook.bookName = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Author"])
    {
        currentBook.bookAuthor = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Narrator"])
    {
        currentBook.bookNarrator = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"OrderDateAsText"])
    {
        currentBook.bookOrderDate = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Description"])
    {
        currentBook.bookDescription = currentNodeContentBook;
        //NSLog(@"Description: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"TotalDurationAsSec"])
    {
        currentBook.bookDuration = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"TotalSizeAsByte"])
    {
        currentBook.bookSizeAtByte = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"ListImageURL"])
    {
        currentBook.listImageUrl = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"DetailImageURL"])
    {
        currentBook.detailImageUrl = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"PlayerImageURL"])
    {
        currentBook.playerImageUrl = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }

    if ([elementname isEqualToString:@"ImageSign"])
    {
        currentBook.imageSign = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }

   else if ([elementname isEqualToString:@"Chapters"])
    {
        if ([elementname isEqualToString:@"Id"])
        {
            currentBook.chapterId = currentNodeContentChapters;
            NSLog(@"chapterId: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");

        }
        if ([elementname isEqualToString:@"Name"])
        {
            currentBook.chapterName = currentNodeContentChapters;
            NSLog(@"chapterName: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }

        if ([elementname isEqualToString:@"Sort"])
        {
            currentBook.chapterSort = currentNodeContentChapters;
            NSLog(@"chapterSort: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }
        if ([elementname isEqualToString:@"DurationAsSec"])
        {
            currentBook.chapterDurationAsSec = currentNodeContentChapters;
            NSLog(@"DurationAsSec: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }

        if ([elementname isEqualToString:@"DownloadURL"])
        {
            currentBook.chapterDownloadUrl = currentNodeContentChapters;
            NSLog(@"chapterDownloadUrl: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }


    }


    if ([elementname isEqualToString:@"MemberBookDTO"])
    {
        [bookListArray addObject:currentBook];
        currentBook = nil;
        currentNodeContentBook = nil;
    }

    else if ([elementname isEqualToString:@"Chapters"])
    {
        currentNodeContentChapters=nil;
    }
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{


*currentNodeContentBook = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        currentNodeContentChapters = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];*
    }
@end

I want to parser both the parent and child tag contents. But The Child tag contents are are found null. I looked various posts and tutorials but undone.Can any one help me please


Solution

  • Add an Log on the didStartElement method and see if all the tags are logged.It will be listed properly and you have to extract it in the rightway

       NSLog(@"\n%@", elementname);
    

    See how the parser works first adding this tag.It is not in the parent child way the parser parse the data.It is just First come first serve way the values approch aand you have to write the logic supporting that

    For more information on parsing Read this document by apple