im trying to parse the child data which is sub_category and show it, but will only show the relevant sub_category of the parent category. I was sucessful in parsing the data of the parent element but im having a problem on how to parse the child element.
- (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 like 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 been researching about the Event Driven XML Parsing and also find a gud reference from one of the threads xml-parse-only-certain-child-elements, but at the end im still pretty confuse about the XML and parsing stuff. I might need a lamer term. And would like to know on how to do my parsing part.
I hope you can use ios native xml parser itself. Your XML looks pretty simple to handle. Just try to create Dictionary for each node and put it in array. So finally you will get array of dictionaries from which you can easily iterate your data.
Create NSMutableArray *dataArray & NSMutableDictionary *dataDict & NSMutableDictionary *subcatDict NSMutableArray *subCatArray
, Also have one NSString
in .h file named 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 out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With