NSXMLParser ios - 如何解析子节点

时间:2016-01-25 08:48:24

标签: ios nsxmlparser

我有一个xml格式,这是它的一部分..

<a:IngredientItemIds xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
enter<b:int>206932</b:int><b:int>206930</b:int></a:IngredientItemIds><a:IsGiftCard>false</a:IsGiftCard>

..等等..

我的问题是XML解析器为元素“IngredientItemIds”取“none” 相反,我想要的是所有“int”标签的数组..

我使用的是NSXML解析器的创建字符,didstartElement和didEndElement的方法

有没有人知道如何解析NSXMLparser中的子节点?

感谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

        // If the current element name is equal to "a:IngredientItemId " then initialize the temporary dictionary.

        if ([elementName isEqualToString:@"a:IngredientItemIds"]) {
            self.dictTempDataStorage = [[NSMutableDictionary alloc]init];
        }

        // Keep the current element.
        self.currentElement = elementName;
    }

    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

        if ([elementName isEqualToString:@"a:IngredientItemIds"]) {
           [self.arrNeighboursData addObject:[[NSDictionary alloc] initWithDictionary:self.dictTempDataStorage]];
        }
        else if ([elementName isEqualToString:@"b:int"]){
            // If the b:int element was found then store it.
            [self.dictTempDataStorage setObject:[NSString stringWithString:self.foundValue] forKey:@"b:int"];
        }

        // Clear the mutable string.
        [self.foundValue setString:@""];
    }


    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        // Store the found characters if only we're interested in the current element.
        if ([self.currentElement isEqualToString:@"b:int"] ) {

            if (![string isEqualToString:@"\n"]) {
                [self.foundValue appendString:string];
            }
        }
    }