2

How do I delete an attribute on an XML node in E4X? I thought this would be easier but I haven't found any examples.

I tried:

delete xml.attribute("myAttribute");

which gives me the following error:

TypeError: Error #1119: Delete operator is not supported with operand of type XMLList.

and I've tried:

xml.attribute("myAttribute") = null;

which causes a compiler error.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

1 Answers1

3

In you example just add the [0] in the resulted XMLList, to ensure you delete a single attribute node, it should work:

delete xml.@attributename[0];

Actually, for me delete with XMLList works as well for simple cases (without complex e4x search constructions):

    var x:XML = <x><c a="1"/><c a="2"/></x>;
    trace("0", x.toXMLString());
    delete x.c.@a;
    trace("1", x.toXMLString());

output:

[trace] 0 <x>
[trace]   <c a="1"/>
[trace]   <c a="2"/>
[trace] </x>
[trace] 1 <x>
[trace]   <c/>
[trace]   <c/>
[trace] </x>
fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • My attribute is on the root node. If you put `a` on `x` do you get the error that I mention in the OP? – 1.21 gigawatts Mar 26 '14 at 08:18
  • for root attributes this works the same for me as in my example, that is all fine (I checked on SDK 3.6A and SDK 4.6). Which SDK do you use? – fsbmain Mar 26 '14 at 08:33
  • I'm using 4.6 as well. It works now. I compared with an earlier version and I had tried `delete xml.attribute("myAttribute");` but didn't try `delete xml.@myAttribute;` like in your answer which works. Thanks – 1.21 gigawatts Mar 26 '14 at 09:02