2011-11-07 48 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

有些人觉得主要是链接的答案是便便。我不是那些人中的一员,但之前我已将我的回答降级为评论。 –