Here is my XML file :
<MemberBooks>
<MemberBookDTO>
<BookId>1</BookId>
<Name>Dönüş</Name>
<Author>Ayşe Kulin</Author>
<Narrator>Ayşe Kulin</Narrator>
<Chapters>
<ChapterDTO>
<Id>1</Id>
<Name>Enrico</Name>
<DurationAsSec>170</DurationAsSec>
</ChapterDTO>
<ChapterDTO>
<Id>2</Id>
<Name>Macias</Name>
<DurationAsSec>176</DurationAsSec>
</ChapterDTO>
<ChapterDTO>
<Id>3</Id>
<Name>Rio</Name>
<DurationAsSec>180</DurationAsSec>
</ChapterDTO>
</Chapters>
</MemberBookDTO>
<MemberBookDTO>
<BookId>2</BookId>
<Name>Flora</Name>
<Author>Kulin</Author>
<Narrator>Ayşe</Narrator>
<Chapters>
<ChapterDTO>
<Id>4</Id>
<Name>Coco</Name>
<DurationAsSec>270</DurationAsSec>
</ChapterDTO>
<ChapterDTO>
<Id>5</Id>
<Name>Dominic</Name>
<DurationAsSec>126</DurationAsSec>
</ChapterDTO>
<ChapterDTO>
<Id>6</Id>
<Name>Margareiti</Name>
<DurationAsSec>130</DurationAsSec>
</ChapterDTO>
<ChapterDTO>
<Id>7</Id>
<Name>Vito</Name>
<DurationAsSec>150</DurationAsSec>
</ChapterDTO>
</Chapters>
</MemberBookDTO>
</MemberBooks>
My code:
- (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];
isInChapterDTO = false;
}
else if ([elementname isEqualToString:@"ChapterDTO"])
{
//currentBook = [[BookListInfo alloc] init];
currentNodeContentChapters=[[NSMutableString alloc] init];
isInChapterDTO = true;
currentChapter = [[Chapters alloc] init];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if(isInChapterDTO==TRUE)
{
if ([elementname isEqualToString:@"Id"])
{
currentChapter.chapterId = currentNodeContentChapters;
}
if ([elementname isEqualToString:@"Name"])
{
NSString*appendString=@"B";
currentNodeContentChapters=[appendString stringByAppendingString:currentNodeContentChapters];
currentChapter.chapterName = currentNodeContentChapters;
}
if ([elementname isEqualToString:@"DurationAsSec"])
{
currentChapter.chapterDurationAsSec = currentNodeContentChapters;
}
}
else
{
if ([elementname isEqualToString:@"BookId"])
{
[currentNodeContentBook stringByAppendingString:currentNodeContentBook];
currentBook.bookId = currentNodeContentBook;
}
if ([elementname isEqualToString:@"Author"])
{
......................
}
if ([elementname isEqualToString:@"Narrator"])
{
......................
}
}
}
if ([elementname isEqualToString:@"MemberBookDTO"])
{
[bookListArray addObject:currentBook];
currentBook = nil;
currentNodeContentBook = nil;
}
else if ([elementname isEqualToString:@"ChapterDTO"])
{
[chapterListArray addObject:currentChapter];
//currentBook.ChapterList.Add(currentChapter);
currentChapter = nil;
isInChapterDTO = false;
}
Here i am parsing this two tag: <MemberBookDTO> & <ChapterDTO>
in two different array :
- "bookListArray" which got two array with their properties.
- "chapterListArray" got 7 array with their properties.
Now i want to count "The number of <ChapterDTO> tags in each <Chapters>"
. How that could be possible? And one another thing is, when i store the value into two array "bookListArray" & "chapterListArray". How can i determine which ChapterDTO
is under which MemberBookDTO
?
If any one have any idea, please share with me.
- Thanks in advance.