NSXMLParser ios - 如何解析子节点
NSXMLParser ios - How to parse child nodes
我有一个 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 parser foundcharacters、didstartElement 和 didEndElement 的方法
有谁知道如何解析 NSXMLparser 中的子节点吗?
谢谢。
试试这个:
-(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];
}
}
}
我有一个 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 parser foundcharacters、didstartElement 和 didEndElement 的方法
有谁知道如何解析 NSXMLparser 中的子节点吗?
谢谢。
试试这个:
-(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];
}
}
}