ios - Use TBXML to grab an unknown number of elements -
my question is: how extract unknown number of xml elements predictable element name , grab attribute values?
i'm using tbxml (love far) parse weather observation data national weather service.
here example of xml (the <metar> element pertinent part)
<metar> <raw_text> kden 181953z 01006kt 10sm few080 sct110 sct150 31/06 a3007 rmk ao2 slp093 ocnl ltgcg dsnt sw virga s-sw cb dsnt sw mov e t03110061 </raw_text> <station_id>kden</station_id> <observation_time>2014-08-18t19:53:00z</observation_time> <latitude>39.85</latitude> <longitude>-104.65</longitude> <temp_c>31.1</temp_c> <dewpoint_c>6.1</dewpoint_c> <wind_dir_degrees>10</wind_dir_degrees> <wind_speed_kt>6</wind_speed_kt> <visibility_statute_mi>10.0</visibility_statute_mi> <altim_in_hg>30.070866</altim_in_hg> <sea_level_pressure_mb>1009.3</sea_level_pressure_mb> <quality_control_flags> <auto_station>true</auto_station> </quality_control_flags> <sky_condition sky_cover="few" cloud_base_ft_agl="8000"/> <sky_condition sky_cover="sct" cloud_base_ft_agl="11000"/> <sky_condition sky_cover="sct" cloud_base_ft_agl="15000"/> <flight_category>vfr</flight_category> <metar_type>metar</metar_type> <elevation_m>1640.0</elevation_m> </metar> the important part <sky_condition> elements , attributes.
as weather changes number of <sky_condition> elements change. there may few one, , many lots.
i display them in order of iteration; first <sky_condition> element the important, followed next, ect.
i'm using traverseelement method iterate on xml. need grab attributes of many <sky_condition> elements there , store them display.
here traverseelement method implementation. passing argument metarelement represents xml pasted above.
- (void) traverseelement:(tbxmlelement *)element { { // display name of element nslog(@"%@",[tbxml elementname:element]); // obtain first attribute element tbxmlattribute * attribute = element->firstattribute; while (attribute) { // display name , value of attribute log window nslog(@"%@->%@ = %@", [tbxml elementname:element], [tbxml attributename:attribute], [tbxml attributevalue:attribute]); // obtain next attribute attribute = attribute->next; } // if element has child elements, process them if (element->firstchild) [self traverseelement:element->firstchild]; // obtain next sibling element } while ((element = element->nextsibling)); } i know need extract attribute values <sky_condition> elements , store them in array (arrays?), can't head around conditionals need write accomplish this.
any or hints appreciated.
i accomplished creating nsmutablearrays store attributes need. in @interface:
@property (nonatomic) bool isskyclear; @property (strong, nonatomic) nsmutablearray *fewarray; @property (strong, nonatomic) nsmutablearray *sctarray; @property (strong, nonatomic) nsmutablearray *bknarray; @property (strong, nonatomic) nsmutablearray *ovcarray; and method implementation - (void) traverseelement:(tbxmlelement *)element {:
{ // display name of element nslog(@"%@",[tbxml elementname:element]); // obtain first attribute element tbxmlattribute * attribute = element->firstattribute; while (attribute) { // display name , value of attribute log window nslog(@"%@->%@ = %@", [tbxml elementname:element], [tbxml attributename:attribute], [tbxml attributevalue:attribute]); if ([[tbxml attributevalue:attribute] isequaltostring:@"clr"]) { self.isskyclear = yes; } else if ([[tbxml attributevalue:attribute] isequaltostring:@"few"]) { attribute = attribute->next; nsstring *cloudbase = [tbxml attributevalue:attribute]; [self.fewarray addobject:cloudbase]; break; } else if ([[tbxml attributevalue:attribute] isequaltostring:@"sct"]) { attribute = attribute->next; nsstring *cloudbase = [tbxml attributevalue:attribute]; [self.sctarray addobject:cloudbase]; break; } else if ([[tbxml attributevalue:attribute] isequaltostring:@"bkn"]) { attribute = attribute->next; nsstring *cloudbase = [tbxml attributevalue:attribute]; [self.bknarray addobject:cloudbase]; break; } else if ([[tbxml attributevalue:attribute] isequaltostring:@"ovc"]) { attribute = attribute->next; nsstring *cloudbase = [tbxml attributevalue:attribute]; [self.ovcarray addobject:cloudbase]; break; } attribute = attribute->next; } // obtain next sibling element } while ((element = element->nextsibling)); }
Comments
Post a Comment