I use NSXMLParser but i have some elements that they have the same name. So i want to know how can i parse them differently?
<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>
Here's a rough example:
-(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;
}
}
then:
-(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;
}
}