Search code examples
apache-flexactionscript-3flex4flex3flashbuilder4

how can i access Children nodes by attributes?


I am trying to create an image gallery :

<card>
 <product catalog="Thread Works">
    <name>AK E001</name>    
    <price>45</price>
    <path>assets\cards\AK_E001.jpg</path>
 </product>

 <product catalog="Paper Work">
     <name>AK E001</name>   
    <price>45</price>
    <path>assets\cards\AK_PP003.jpg</path>
 </product>

<product catalog="Thread Works">
    <name>AK E002</name>    
    <price>50</price>
    <path>assets\cards\AK_E002.jpg</path>
 </product>

I can access the attribute, but if i am selecting "Thread Works" i want only access <product catalog="Thread Works"> with children node , for additem to array collection any Help Thanks


Solution

  • <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx">
        <fx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.events.FlexEvent;
                private var mainXML:XML = <card>
                                             <product catalog="Thread Works">
                                                <name>AK E001</name>    
                                                <price>45</price>
                                                <path>assets\cards\AK_E001.jpg</path>
                                             </product>
    
                                             <product catalog="Paper Work">
                                                 <name>AK E001</name>   
                                                <price>45</price>
                                                <path>assets\cards\AK_PP003.jpg</path>
                                             </product>
    
                                            <product catalog="Thread Works">
                                                <name>AK E002</name>    
                                                <price>50</price>
                                                <path>assets\cards\AK_E002.jpg</path>
                                             </product>
                                            </card>;
    
                protected function fetchHandler(event:MouseEvent):void
                {
                    var productAC:ArrayCollection = new ArrayCollection();
                    var productList:XMLList = mainXML.product.(@catalog == attributeNameTxt.text);
                    for each(var item:XML in productList)
                    {
                        productAC.addItem({name:String(item.name[0]),
                            price:String(item.price[0]),
                            path:String(item.path[0])});
                    }
                    trace(productAC);
                }
    
            ]]>
        </fx:Script>
        <s:layout>
            <s:HorizontalLayout/>
        </s:layout>
        <s:TextInput id="attributeNameTxt" text="Thread Works"/>
        <s:Button click="fetchHandler(event);"/>
    </s:WindowedApplication>
    
    IMPORTANT CODE : 
    =================
    mainXML.product.(@catalog == "Thread Works")