2011-09-08 66 views
0

我想創建一個圖片庫:我如何通過屬性訪問兒童節點?

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

我可以訪問屬性,但如果我選擇「主題作品」我只希望有孩子的節點訪問 <product catalog="Thread Works">,爲的AddItem數組收集任何幫助感謝

回答

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") 
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 : [email protected], 
      name : xml..name, 
      price : xml..price, 
      path : xml..path 
     }); 
    } 
    for(i=0;i<dataContainer.length;i++) 
    { 
      if(dataContainer[i].catalog[0]== "your String"){ 
       showName(); 
      } 
    } 
} 
+0

我只想跟蹤「線程作品」不是所有的XML。 所有<產品目錄=「線程工作」>與兒童。 – Rajkamal

+0

'trace(dataContainer [0] .catalog [0]);'設置數組的位置。 – Benny

+0

我想添加只有「線程作品」<產品目錄=「線程作品」>和兒童陣列 – Rajkamal