Xml – How to retrieve the node name from XML in Flex / Actionscript

actionscript-3apache-flexxml

I have some data xml data that looks like this

<root xsi:noNamespaceSchemaLocation="test1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <configuration>
    <CLICK/>
    <KLT/>
    <DETd/>
  </configuration>
</root>

I get a list of the configurations using

var results:XMLList = xml.configuration.*;

Now I want to loop over the XMLList and output CLICK, KLT, DETd etc. but how in XML do I get the node name as such

Best Answer

XML:

<root>
    <parentNode>
        <childNode1>1</childNode1>
        <childNode2>2</childNode2>
        <childNode3>3</childNode3>
    </parentNode>
</root>

All you need to do is use .name() while iterating through parentNode's children().

for(var i:int=0;i<xml.children()[0].children().length();i++)
{
    var currentNode:XML = xml.children()[0].children()[i];
    trace(currentNode.name());
}

//childNode1
//childNode2
//childNode3

More:

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