1
<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 the 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 3th item

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91

2 Answers2

4

The reason it's blowing up on the third item is that 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: Brackets after name were missing.

Community
  • 1
  • 1
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

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

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