Vishwas
Vishwas

Reputation: 1541

How to populate flex combo box with xml data

here is the xml data :

<root>
    <recommendedMaterials>
        <value label="Aluminium" data="0" /> 
        <value label="Iron" data="0" /> 
    </recommendedMaterials>

</root

My code :

<mx:Script>
        <![CDATA[
public function populateRecommendedMaterials(xml_val:XML)           
            {
                 materials_Cmb.dataProvider=(xml_val.recommendedMaterials); 
            }

    ]]>

</mx:Script>        

    <mx:ComboBox x="212" y="164" id="materials_Cmb" dataProvider="materialsCmb_Dp"></mx:ComboBox>

</mx:Canvas>

The problem is that the whole xml gets populated. I just want the labels. :(

Upvotes: 1

Views: 1588

Answers (1)

RIAstar
RIAstar

Reputation: 11912

There are two approaches here, depending on what you need. In either case the data you want are the children of the recommendedMaterials node, not the node itself (which is what you did).

materials_Cmb.dataProvider = 
    new XMLListCollection(xml_val.recommendedMaterials.children());

This should already do the trick. Note that I wrapped the XMLList in an XMLListCollection: this is not strictly necessary with the mx:ComboBox, because it will do the same internally, but for Spark components it would be mandatory.

Another more concise solution would be to just find all the 'value' nodes, but I don't know whether that approach fits your bill.

materials_Cmb.dataProvider = 
    new XMLListCollection(xml_val..value);

Also don't forget to assign the correct 'labelField' in the ComboBox:

<s:ComboBox labelField="@label" />

The @ sign represents an XML attribute.

Upvotes: 4

Related Questions