简体   繁体   中英

Flex/ActionScript - XML

I am trying to read the below xml file and get the name-value pairs in Flex / Actionscript.

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

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 ?

Thanks in advance

You need to use e4x which is ECMAScript for XML. e4x, much like XPath, can traverse through the XML document to find elements.

Here are some articles by 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

Look at the samples in the following document.

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:
<!-- comment -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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