Domanda

<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>

Devo ottenere tutti gli attributi (sia chiave che valore)

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

fornisce questo errore

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

sul 3 ° elemento

È stato utile?

Soluzione

Il motivo per cui sta saltando in aria sul terzo elemento è che ha due attributi. Stai utilizzando un collegamento che ottiene il nome solo se esiste un solo attributo. Devi modificare il codice nel modo seguente:

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

Modifica: mancavano le parentesi dopo il nome.

Altri suggerimenti

Usa attr.valueOf () per ottenere il valore di quell'attributo

for each (var item : XML in data.items)
{
    for each (var attr : XML in item.attributes())
    {
        trace(attr.name()+":"+ attr.valueOf());
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top