How do I retrieve the node name from XML in Flex / Actionscript

耗尽温柔 提交于 2019-12-23 04:20:49

问题


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


回答1:


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



回答2:


Just use the name as the accessor.

for each (var prop:XML in xml.configuration.*) 
{ 
    trace(prop.name());
}

You had xml.configuration.* listing what was needed, so you were half-way there. Just take each element as an XML in the iteration (with a for each loop).




回答3:


You can use the name() method on any XML node, like so:

for each(var n:XML in results){
    trace(n.name());
}

Will output:

CLICK
KLT
DETd


来源:https://stackoverflow.com/questions/5163232/how-do-i-retrieve-the-node-name-from-xml-in-flex-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!