Search code examples
actionscripte4x

How do I remove a node with e4x based on a property?


I have a simple example

var myXML:XML =
<root>
    <element type="a">I am a</element>
    <element type="b">I am b</element>
</root>
;

I cant work out how I can programatically remove an element of a specific type

delete myXML.root.element.type['a'][0];

Solution

  • To remove a XML element by matching an attribute of that element, I believe you're looking for:

    var index:int = myXML.element.(@type=="b").childIndex();
    delete myXML.element[index];
    

    Based upon your XML:

    var myXML:XML =
        <root>
            <element type="a">I am a</element>
            <element type="b">I am b</element>
        </root>;
    

    After calling this function, the XML would be:

    <root>
      <element type="a">I am a</element>
    </root>