在ios中计算xml多个子标记

时间:2013-11-20 06:26:55

标签: ios objective-c xml nsxmlparser

我的XML看起来像这样:

<a>
   <b>b1</b>
   <c>c1</c>
   <d>
       <x>apple1</x>
       <y>orange1</y>
       <z>Mango1</z>
   </d>
   <d>
       <x>apple2</x>
       <y>orange2</y>
       <z>Mango2</z>
   </d>
   <d>
       <x>apple3</x>
       <y>orange3</y>
       <z>Mango3</z>
   </d>
</a>
<a>
   <b>b2</b>
   <c>c2</c>
   <d>
       <x>apple4</x>
       <y>orange4</y>
       <z>Mango4</z>
   </d>
   <d>
       <x>apple5</x>
       <y>orange5</y>
       <z>Mango5</z>
   </d>
</a>

在这里,我想计算objectiveC中每个<d>...</d><a>...</a>个标记的数量。第一个<a>标记的答案为3,第二个<a>标记的答案为2。请给我一个建议。感谢。

这是我的代码:

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"a"])
    {
        currentBook = [[BookListInfo alloc] init];
        currentNodeContentBook=[[NSMutableString alloc] init];
        isInChapterDTO = false;
    }
    else if ([elementname isEqualToString:@"d"])
    {
    //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:@"x"])
        {
            currentChapter.chapterId = currentNodeContentChapters;
        }
        if ([elementname isEqualToString:@"y"])
        {
            NSString*appendString=@"B";
            currentNodeContentChapters=[appendString   stringByAppendingString:currentNodeContentChapters];
        currentChapter.chapterName = currentNodeContentChapters;
        }
        if ([elementname isEqualToString:@"z"])
        {
            currentChapter.chapterDurationAsSec = currentNodeContentChapters;
        }
}
    else
    {
        if ([elementname isEqualToString:@"b"])
        {
            [currentNodeContentBook stringByAppendingString:currentNodeContentBook];
        currentBook.bookId = currentNodeContentBook;
        }
        if ([elementname isEqualToString:@"c"])
        {
            ......................
        }
    }
}
if ([elementname isEqualToString:@"a"])
{
    [bookListArray addObject:currentBook];
    currentBook = nil;
    currentNodeContentBook = nil;
}
else if ([elementname isEqualToString:@"d"])
{
    [chapterListArray addObject:currentChapter];
    //currentBook.ChapterList.Add(currentChapter);
    currentChapter = nil;
    isInChapterDTO = false;
}

2 个答案:

答案 0 :(得分:3)

试试这个:

int dCount;
BOOL isATag;

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"a"])
    {
        isATag = YES;
    }
    else if ([elementname isEqualToString:@"d"])
    {
        if(isATag){
            dCount++;
        }
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   if ([elementname isEqualToString:@"a"]){
        NSLog(@"d tag count between A: %d",dCount);
        dCount=0;
        isATag = NO;
    }
}

答案 1 :(得分:1)

声明以下对象

int count;
BOOL insideATag;
NSMutableArray *dCounts;

viewDidLoad 中初始化 dCounts

dCounts = [[NSMutableArray alloc]init];

添加以下委托方法

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"a"])
    {
        insideATag = YES;
        count = 0;
    }

}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   if ([element isEqualToString:@"a"]){
        insideATag = NO;
        [dCounts addObject:count]
    } else if ([elementname isEqualToString:@"d"] && insideATag)
    {
        count++;
    }
}

如果你想在第一个'a'标签内计算'd'标签,那么

NSLog(@"Count of d  : %i",[dCounts objectAtIndex:0]);