如何通过属性访问子节点?

时间:2011-09-08 05:52:36

标签: flex actionscript-3 flex4 flex3 flashbuilder4

我正在尝试创建图片库:

<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>

我可以访问该属性,但如果我选择“Thread Works”,我只想访问带有子节点的 <product catalog="Thread Works">,对于数组集合的additem,任何帮助谢谢

3 个答案:

答案 0 :(得分:3)

参考http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_08.html

在你的情况下:

var x:XML; //用你的数据填充Xml

var threadWorks:XML = x.product。(@ catalog ==“Thread Works”);

编辑: 警告:别忘了。在产品和@catalog之间

答案 1 :(得分:0)

function onXmlLoaded (e:Event):void 
{
    xml = XML(e.target.data);
    var xmlLength:Number  = xml.product.length();
    for(var i:uint = 0; i<xmlLength; i++)
    {
            dataContainer.push( {
            catalog : xml.product.@catalog,
            name :  xml..name,
            price : xml..price,
            path : xml..path
        });
    }
    for(i=0;i<dataContainer.length;i++)
    {
           if(dataContainer[i].catalog[0]== "your String"){
                 showName();
           }
    }
}

答案 2 :(得分:0)

<?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")