2011-11-07 47 views
0

我正在嘗試讀取下面的xml文件並獲取Flex/Actionscript中的名稱/值對。Flex/ActionScript - XML

sample.xml中

<ABC> 
    <XYZ>    // First child 
    <id> </id> 
    <width> 10 </width> 
    <height> 10 </height> 
    <name> Person1 </name> 
    </XYZ> 
    <XYZ>    // Second child 
    <id> </id> 
    <width> 20 </width> 
    <height> 20 </height> 
    <name> Person2 </name> 
    </XYZ> 
</ABC> 

我嘗試使用下面的所有變量名和值的詳細信息使用XML

.name().text()功能既孩子的,但我沒有得到它。

我的問題是,

誰能告訴我如何解析這個XML獲得細節每個孩子的 單獨在一個對象,然後將該對象存儲在 陣列供以後使用?

在此先感謝

+0

爲什麼不保持它在XML,並用它作爲源爲XMLListCollection? E4x語法與對象引用語法沒有多大區別。 –

回答

1
 var xml:XML = 
      <ABC> 
       <XYZ>    // First child 
        <id> </id> 
        <width> 10 </width> 
        <height> 10 </height> 
        <name> Person1 </name> 
       </XYZ> 
       <XYZ> 
        <id> </id> 
        <width> 20 </width> 
        <height> 20 </height> 
        <name> Person2 </name> 
       </XYZ> 
      </ABC>; 
     // Get list of children named XYZ 
     var children:XMLList = xml.XYZ; 
     for each (var child:XML in children) 
     { 
      trace(child.name()); 
      // Get list of inner children (with any name) 
      for each (var prop:XML in child.children()) 
      { 
       // You need text from inner children, so here it is 
       trace("\t" + prop.name() + " = " + prop.text()); 
      } 
     } 

請注意,您的評論(//第一個孩子)將成爲在孩子列表爲空名稱文本節點。
評論中XML是這樣的:
<!-- comment -->

3

您需要使用e4x這是用於XML的ECMAScript。 e4x很像XPath,可以遍歷XML文檔來查找元素。

這是Adobe的一些文章。

http://learn.adobe.com/wiki/display/Flex/E4X
http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_03.html

下列文件中看看樣品。

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html

一些教程

Look here for a tutorial
Here's another tutorial

+0

請注意評論爲何答案被拒絕投票? –

+1

有些人覺得主要是鏈接的答案是便便。我不是那些人中的一員,但之前我已將我的回答降級爲評論。 –