ios - Getting data of Child Element from its Parent counterpart -
im trying parse child data sub_category , show it, show relevant sub_category of parent category. sucessful in parsing data of parent element im having problem on how parse 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 xml file 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 been researching event driven xml parsing , find gud reference 1 of threads xml-parse-only-certain-child-elements, @ end im still pretty confuse xml , parsing stuff. might need lamer term. , know on how parsing part.
i hope can use ios native xml parser itself. xml looks pretty simple handle. try create dictionary each node , put in array. array of dictionaries can iterate data.
create nsmutablearray *dataarray & nsmutabledictionary *datadict & nsmutabledictionary *subcatdict nsmutablearray *subcatarray
, have 1 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 out.
Comments
Post a Comment