问题
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.
回答1:
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.
回答2:
Convert to an internal class, create a populate( data:XML ) and toXML():XML method. You will most likely have other methods in there.
来源:https://stackoverflow.com/questions/18433396/as3-xml-delete-node