代码之家  ›  专栏  ›  技术社区  ›  1.21 gigawatts

如何使用E4X删除特定类型的所有节点?

  •  1
  • 1.21 gigawatts  · 技术社区  · 9 年前

    我有一个XML实例,它有许多节点需要删除。我知道如何获取这些节点的列表,但如何删除它们?之后,我将不得不向XMLList中添加新项。不确定这是否会改变答案。我看到XMLList上有一个replace()方法。

    操作脚本:

    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>
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   1.21 gigawatts    9 年前

    好的。我找到了一些方法。

    方法1。使用XMLListCollection。这将从原始XML中删除项目:

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

    方法2。直接从XML中删除节点:

    delete myXML.Hyperlink;
    

    然后,我可以使用XMLListCollection.addItem()再次添加它们:

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