<...">

AS3: all key values ​​+ from XML attributes

<top>
    <item link="http://www.google.be"><![CDATA[test]]></item>
    <item link="http://www.google.be"><![CDATA[test]]></item>
    <item bold="true" link="http://www.google.be"><![CDATA[test]]></item>
</top>

      

I need to get all attributes (both key and value)

for each ( var item : XML in data.item )
{
     trace(item.attributes().name());
}

      

gives this error

 TypeError: Error #1086: The name method only works on lists containing one item.

      

on the 3rd element

+1


source to share


2 answers


The reason it enters into the third element is because it has two attributes. You are using a shortcut that only gets the name if there is only one attribute. You need to change your code to the following:

for each (var item : XML in data.items)
{
    for each (var attr : XML in item.attributes())
    {
        trace(attr.name());
    }
}

      



Edit: There are no parentheses after the name.

+4


source


Use attr.valueOf () to get the value of this attribute



for each (var item : XML in data.items)
{
    for each (var attr : XML in item.attributes())
    {
        trace(attr.name()+":"+ attr.valueOf());
    }
}

      

+1


source







All Articles