简体   繁体   中英

AS3 XML delete node

I have this XML structure:

<dbase>
   <employee>
      <Name>NAME</Name>
      <Surname>SURNAME</Surname>
      <Company>COMPANY</Company>
      <Date>DATE</Date>
      <Compare>1377390433625</Compare>
   </employee>
</dbase>

I'd like to know how to search for the Compare value an delete the employee if there is a match.

I'll appreciate any feedback.

Maybe the following code will be useful. Assuming that the xml structure will be like this:

        <dbase>
        <employee>
            <Name>NAME</Name>
            <Surname>SURNAME</Surname>
            <Company>COMPANY</Company>
            <Date>DATE</Date>
            <Compare>1377390433625</Compare>
        </employee>
        <employee>
            <Name>WILL BE DELETED</Name>
            <Surname>SURNAME</Surname>
            <Company>COMPANY</Company>
            <Date>DATE</Date>
            <Compare>1234</Compare>
        </employee>
        <employee>
            <Name>NAME</Name>
            <Surname>SURNAME</Surname>
            <Company>COMPANY</Company>
            <Date>DATE</Date>
            <Compare>34878937</Compare>
        </employee>         
    </dbase>

In the next code, i will delete the node with Compare = 1234 :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
    <mx:Script>
        <![CDATA[

            public function init():void{
                delete myXML.employee.(elements('Compare') == '1234')[0];
                txt.text = myXML.toString();
            }
        ]]>
    </mx:Script>
    <mx:XML id="myXML">
        <dbase>
            <employee>
                <Name>NAME</Name>
                <Surname>SURNAME</Surname>
                <Company>COMPANY</Company>
                <Date>DATE</Date>
                <Compare>1377390433625</Compare>
            </employee>
            <employee>
                <Name>WILL BE DELETED</Name>
                <Surname>SURNAME</Surname>
                <Company>COMPANY</Company>
                <Date>DATE</Date>
                <Compare>1234</Compare>
            </employee>
            <employee>
                <Name>NAME</Name>
                <Surname>SURNAME</Surname>
                <Company>COMPANY</Company>
                <Date>DATE</Date>
                <Compare>34878937</Compare>
            </employee>         
        </dbase>
    </mx:XML>
    <mx:TextArea id="txt" width="400" height="400" />
</mx:Application>

Basically this line delete the node that matches with the number as i want (in this case, 1234 ):

delete myXML.employee.(elements('Compare') == '1234')[0];

You can try this here LINK . I hope this will be useful.

(Edited) 2013-08-26 : Here the LINK for download the project. This file is a .fxp file, you can import this, click in File --> Import... --> Flash Builder Project.. --> select the .fxp file .

Convert to an internal class, create a populate( data:XML ) and toXML():XML method. You will most likely have other methods in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM