Search code examples
xmlactionscript-3e4x

How to remove all nodes of a certain type using E4X?


I have an XML instance that has numerous nodes that I want to remove. I know how to get a list of those nodes but how would I remove them? I will have to add new items to the XMLList afterwards. Not sure if that changes the answer or not. I saw there is a replace() method on XMLList.

ActionScript:

var xmllist:XMLList = myXML.Hyperlink;

XML:

<Document>
    <HyperlinkURLDestination Self="HyperlinkURLDestination/http%3a//test.com#1stMultilineLink/" DestinationUniqueKey="1" Name="http://test.com#1stMultilineLink/" DestinationURL="http://test.com#1stMultilineLink/" Hidden="false"/>
    <HyperlinkURLDestination Self="HyperlinkURLDestination/http%3a//test.com#inlinehyperlink_noshareddestination" DestinationUniqueKey="2" Name="http://test.com#inlinehyperlink_noshareddestination" DestinationURL="http://test.com#inlinehyperlink_noshareddestination" Hidden="true"/>
    <HyperlinkURLDestination Self="HyperlinkURLDestination/http%3a//google.com#multilinehyperlink" DestinationUniqueKey="3" Name="http://google.com#multilinehyperlink" DestinationURL="http://google.com#multilinehyperlink" Hidden="false"/>
    <Hyperlink Self="ufc" Name="is a  multiline hyperlink that terminates here" Source="uf9" Visible="false" Highlight="None" Width="Thin" BorderStyle="Solid" Hidden="false" DestinationUniqueKey="1">
        <Properties>
            <BorderColor type="enumeration">Black</BorderColor>
            <Destination type="object">HyperlinkURLDestination/http%3a//test.com#1stMultilineLink/</Destination>
        </Properties>
    </Hyperlink>
    <Hyperlink Self="u112" Name="hyperlink inline" Source="u111" Visible="false" Highlight="None" Width="Thin" BorderStyle="Solid" Hidden="false" DestinationUniqueKey="2">
        <Properties>
            <BorderColor type="enumeration">Black</BorderColor>
            <Destination type="object">HyperlinkURLDestination/http%3a//test.com#inlinehyperlink_noshareddestination</Destination>
        </Properties>
    </Hyperlink>
    <Hyperlink Self="u137" Name="another multline hyperlink" Source="u136" Visible="false" Highlight="Outline" Width="Thick" BorderStyle="Solid" Hidden="false" DestinationUniqueKey="3">
        <Properties>
            <BorderColor type="enumeration">Purple</BorderColor>
            <Destination type="object">HyperlinkURLDestination/http%3a//google.com#multilinehyperlink</Destination>
        </Properties>
    </Hyperlink>
</Document>

Solution

  • OK. I've found a few methods.

    Method 1. Using an XMLListCollection. This appears to remove the items from the original XML:

    var hyperlinksList:XMLList = myXML.Hyperlink;
    var myXMLListCollection:XMLListCollection = new XMLListCollection(hyperlinksList);
    myXMLListCollection.removeAll();
    

    Method 2. Deleting the nodes directly from the XML:

    delete myXML.Hyperlink;
    

    I can then add them back again with XMLListCollection.addItem():

    myXMLListCollection.addItem(<Hyperlink id="mylink"/>);