2012-08-27 82 views
0

可能重複:
how I get child node from each parent node seperately?如何從Xml數據獲取所有的子節點?

我有一些XML data..I要創建基於此XML.My XML數據中的某些組件的下方

<main> 
    <TabNavigator x="27" y="11" width="455" height="376" id="gh" backgroundColor="#A4B6E9">               <NavigatorContent width="100%" height="100%" label="Client" id="clientTab"> 
    <Label x="10" y="30" width="52" height="25" text="Name:"/> 
    <Label x="10" y="127" width="52" height="28" text="Addres"/> 
<TextInput id="name_client" x="69" y="18" width="188" height="37" restrict="[A-Z a-z]"/> 
<TextArea id="address_client" x="70" y="70" height="126"/> 
<Label x="10" y="230" width="84" height="32" text="Phone:"/> 
<TextInput id="phone_client" x="70" y="218" width="188" height="30" restrict="0-9" maxChars="10"/> 
<Button x="100" y="291" height="28" label="Submit" click="submitClick()"/> 
<Label id="errorClient" x="59" y="270" width="171" height="27" text="please fill the blank fields" color="red" visible="false"/> 
</NavigatorContent><NavigatorContent width="100%" height="100%" label="Admin" id="adminTab"> 
<Label x="23" y="48" width="52" height="25" text="Name:"/> 
<Label x="26" y="148" width="52" height="28" text="Addres"/><TextInput id="name_admin" x="105" y="33" width="188" height="37"/> 
<TextArea id="address_admin" x="105" y="93" height="126"/> 
<Label x="26" y="257" width="84" height="32" text="Phone:"/> 
<TextInput id="phone_admin" x="104" y="246" width="188" height="30" restrict="0-9" maxChars="10"/> 
<Button x="137" y="305" height="28" label="Submit"/> 
<Label id="errorAdmin" x="100" y="286" width="171" height="17" color="red" fontSize="14" text="please fill the blank fields" visible="false"/> 
<Button x="335" y="60" height="34" label="Admin Details"/> 
<Button x="335" y="180" height="34" label="Client Details"/> 
</NavigatorContent> 
</TabNavigator> 
<TitleWindow x="521" y="84" width="377" height="234"> 
<DataGrid x="0" y="0" width="375" height="163" borderVisible="true" id="details"> 
<columns> 
<ArrayList> 
<GridColumn dataField="Name" id="arrayName"/> 
<GridColumn dataField="Address" headerText="Address"/> 
    <GridColumn dataField="Phone_Number" headerText="Phone_Number"/></ArrayList></columns> 
    </DataGrid><Button x="139" y="167" height="28" label="Export"/> 
</TitleWindow> 
</main> 
給出

我正在使用以下代碼來查找上述XML中的子節點。

private function loadXML(targetURL:String):void 
       { 
        urlLdr.load(new URLRequest(targetURL)); 
        urlLdr.addEventListener(Event.COMPLETE,urlLdr_complete); 
       } 
       private function urlLdr_complete(event:Event):void 
       { 

        var xmlData:XML=new XML(URLLoader(event.currentTarget).data);      
         for each (var t:XML in xmlData.children()) 
         { 
              Alet.show(t.Name()); 
              } 
          }} 

但我只有2個子節點(TabNavigator和NavigatorContent)。我如何得到所有的孩子節點?任何人都可以幫我嗎?


我的代碼如下。我唯一的孩子節點現在..還沒有得到父母幫助node.please我要獲取父和子節點..

<fx:Script> 
     <![CDATA[ 
      import flashx.textLayout.elements.BreakElement; 
      import flashx.textLayout.formats.BackgroundColor; 

      import mx.charts.chartClasses.DataDescription; 
      import mx.collections.ArrayCollection; 
      import mx.collections.XMLListCollection; 
      import mx.containers.Canvas; 
      import mx.containers.TabNavigator; 
      import mx.controls.Alert; 
      import mx.controls.Button; 
      import mx.controls.Label; 
      import mx.controls.Text; 




      private var urlLdr:URLLoader=new URLLoader; 
      private var childName:String; 
      private var i:int=0; 
      private var arrayCollection:ArrayCollection=new ArrayCollection; 
      private function loadXML(targetURL:String):void 
      { 
       urlLdr.load(new URLRequest(targetURL)); 
       urlLdr.addEventListener(Event.COMPLETE,urlLdr_complete); 
      } 
      private function urlLdr_complete(event:Event):void 
      { 

       var xmlData:XML=new XML(URLLoader(event.currentTarget).data); 
       handleOneNode(xmlData); 
      } 
      private function handleOneNode(node:XML,parent:XML=null):void 
      { 
      var children:XMLList=node.children(); 
      if(children.length()==0) 
      {i++; 
       childName=node.name(); 
       switch(childName.toString()) 
       { 
        case "Button": 
        { 

         var myButton:Button=new Button(); 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         myCanvas.addChild(myButton); 
         break; 
        } 
        case "TabNavigator": 
        { 
         var myTabNavigator:TabNavigator=new TabNavigator(); 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         break; 
        } 
        case "Datechooser": 
        { 
         break; 
        } 
        case "Label": 
        { 
         var myLabel:Label=new Label(); 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         myCanvas.addChild(myLabel); 
        } 
        case "TextInput": 
         var myText:Text=new Text; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         [email protected]; 
         myCanvas.addChild(myLabel); 
        case "TitleWindow": 
        { 
        } 
        default: 
        { 
        } 
       } 

      } 
      else 
      { 
       for each(var child:XML in children) 
       { 
        handleOneNode(child,node); 
       } 
      } 
      } 

     ]]> 
    </fx:Script> 

回答

1

孩子()類XML的方法只返回的直接孩子當前對象。如果你想瀏覽整個文檔,你需要對每個孩子遞歸地調用你的方法。

所以,你需要寫類似:

private function handleOneNode(node:XML):void { 
    var children:XMLList = node.children(); 
    if (children.length() == 0) { 
     //Handle Leaf node -> Create ui object, or whatever 
    } else { 
     //Non terminal node, check children 
     for each (var child:XML in children) { 
     handleOneNode(child); 
     } 
    } 
} 

並且處理函數中調用它:

var xmlData:XML=new XML(URLLoader(event.currentTarget).data); 
handleOneNode(xmlData) 

編輯 爲了訪問你只需要添加父節點handleOneNode函數的附加參數。

private function handleOneNode(node:XML, parent:XML=null):void { 
     var children:XMLList = node.children(); 
     if (children.length() == 0) { 
      //Handle Leaf node -> Create ui object, or whatever 
     } else { 
      //Non terminal node, check children 
      for each (var child:XML in children) { 
      handleOneNode(child, node); 
      } 
     } 
    } 

並且處理函數中調用它:

var xmlData:XML=new XML(URLLoader(event.currentTarget).data); 
handleOneNode(xmlData) 
+0

太感謝你了.......這是糾正我的錯誤.. –

+0

這隻能說明孩子名字非常有幫助。我也想要父母的名字..我怎麼得到它? –

+0

@DipinKumar我已經更新了我的答案,將父項傳遞給函數。請注意,您也可以在節點參數上使用.parent()方法(即node.parent()) – Davz

相關問題