简体   繁体   English

Flex / ActionScript-XML

[英]Flex/ActionScript - XML

I am trying to read the below xml file and get the name-value pairs in Flex / Actionscript. 我正在尝试阅读下面的xml文件,并在Flex / Actionscript中获取名称/值对。

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

I am trying to use the following all tag name and value details for both childs using .name() and .text() function of XML 我正在尝试使用XML的.name().text()函数为两个子项使用以下所有标签名称和值详细信息

But I am not getting it. 但是我不明白。

My question is, 我的问题是

Can anyone please tell me how to parse this xml to obtain the details of each child separately in an object and then store that object in array for later use ? 谁能告诉我如何解析此xml以便分别在对象中获取每个子对象的详细信息,然后将该对象存储在数组中以备后用?

Thanks in advance 提前致谢

You need to use e4x which is ECMAScript for XML. 您需要使用e4x ,它是ECMAScript for XML。 e4x, much like XPath, can traverse through the XML document to find elements. e4x就像XPath一样,可以遍历XML文档以查找元素。

Here are some articles by Adobe. 这是Adobe的一些文章。

http://learn.adobe.com/wiki/display/Flex/E4X 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://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_03.html

Look at the samples in the following document. 查看以下文档中的样本。

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

Some tutorials 一些教程

Look here for a tutorial 在这里查看教程
Here's another tutorial 这是另一个教程

        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());
            }
        }

Note that your comment (// First child) will become text node with null name in child list. 请注意,您的注释(//第一个孩子)将成为在孩子列表中具有空名称的文本节点。
Comments in XML are like this: XML注释如下:
<!-- comment -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM