Retrieving child item data from its parent copy

im trying to parse child data that is sub_category and show it, but will only show the corresponding sub_category of the parent category. I managed to process the data of the parent, but I had a problem parsing the child.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if([elementName isEqualToString:@"category"]){ dataCurrent = [dataFileHolder alloc]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { currentList = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"name"]){ dataCurrent.nameOfCat = currentList; } if ([elementName isEqualToString:@"description"]){ dataCurrent.descriptionOfCat = currentList; } if ([elementName isEqualToString:@"image"]) { dataCurrent.imageLink = currentList; } if ([elementName isEqualToString:@"category"]) { [listPopulated addObject:dataCurrent]; dataCurrent = nil; currentList = nil; } } 

and the xml file is similar to this

 <category> <name>Food</name> <description>food description</description> <image> Link Here </image> <sub_cat> <sub_name>Sub name</sub_name> <sub_desc>sub cat description</sub_desc> <sub_image> Link </sub_image> </sub_cat> <sub_cat> <sub_name>Sub name</sub_name> <sub_desc>sub cat description</sub_desc> <sub_image> Link </sub_image> </sub_cat> </category> 

and I studied Event Driven XML Parsing and also found the gud link from one of the xml-parse-only-certain-child-elements streams, but in the end I was still pretty confused about XML and parsing. I may need a lamer term. And I would like to know how to make my parsing part.

+6
source share
2 answers

Hope you can use ios native XML parser. Your XML looks pretty easy to handle. Just try creating a dictionary for each node and put it in an array. So, you will get an array of dictionaries from which you can easily iterate over your data.

Create NSMutableArray *dataArray & NSMutableDictionary *dataDict & NSMutableDictionary *subcatDict NSMutableArray *subCatArray , also have one NSString in the .h file called currentElement .

 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { currentElement = elementName; if([elementName isEqualToString:@"category"]){ dataDict = [[NSMutableDictionary alloc] init]; } else if ([elementName isEqualToString:@"sub_cat"]) { if(!subCatArray) { subCatArray = [[NSMutableArray alloc] init]; } subcatDict = [[NSMutableDictionary alloc] init]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(dataDict && !subCatArray){ [dataDict setObject:string forKey:currentElement]; } else if(subCatArray && subcatDict) { [subcatDict setObject:string forKey:currentElement]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"category"]){ [dataDict setObject:subCatArray forKey:@"sub_cat"]; [dataArray addObject:dataDict]; subCatArray = nil; dataDict = nil; } else if([elementName isEqualToString:@"sub_cat"]){ [subCatArray addObject:subcatDict]; subcatDict = nil; } } 

This will help you.

+7
source

Try using third-party XML parsers, such as RaptureXML .

Then you analyze the data with:

 RXMLElement *rootXML = [RXMLElement elementFromXMLData:yourXMLData]; 

and request the resulting root element:

 NSLog(@"Category name: %@", [rootXML child:@"name"].text); [rootXML iterate:@"image.sub_cat" usingBlock: ^(RXMLElement *e) { NSLog(@"Sub name: %@", [e child:@"sub_name"].text); }]; 
0
source

Source: https://habr.com/ru/post/952494/


All Articles