gpt4 book ai didi

iphone - 解析同名元素?

转载 作者:行者123 更新时间:2023-11-28 23:09:56 26 4
gpt4 key购买 nike

我使用 NSXMLParser 但我有一些元素具有相同的名称。所以我想知道如何以不同方式解析它们?

<hours type="array">
<hour>
<name>Lun - Dim</name>
<value>19:00 - 01:00</value>
</hour>
</hours>
<categories type="array">
<category>
<name>Cuisine indienne</name>
<value>indian</value>
</category>
</categories>

最佳答案

这是一个粗略的例子:

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



if ([elementName isEqualToString:@"hours"]) {
//Tells us we are in hours group
_isInHoursGroup = YES;
//Get our hour object ready
_myHourObject = [MyHourObject alloc] init];
return;
}

if ([elementName isEqualToString:@"categories"]) {
_isInCategoriesGroup = YES;
_myCategoryObject = [MyCategoryObject alloc] init];
return;

}


}

然后:

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




if ([elementName isEqualToString:@"hours"]) {
_isInHoursGroup = NO;
return;
}

if ([elementName isEqualToString:@"categories"]) {
_isInCategoriesGroup = NO;
return;
}

if (_isInHoursGroup && [didEndElement isEqualToString:@"name"]) {

//Assumes you have _currentValue variable with the current value in it.
_myHourObject.myPropert = _currentValue;
return;
}

if (_isInCategoriesGroup && [didEndElement isEqualToString:@"name"]) {

//Assumes you have _currentValue variable with the current value in it.
_myCategoryObject.myPropert = _currentValue;
return;
}



}

关于iphone - 解析同名元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8563034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com