我想从我的xml类动态添加数据到列表。 我的xml类是这样的:
<?xml version="1.0" ?>
<persons>
<person id="1" >
<firstName>Anthony</firstName>
<lastName>Robbins</lastName>
</person>
<person id="2" >
<firstName>Deil</firstName>
<lastName>Carnegie</lastName>
</person>
<person id="3" >
<firstName>Bill</firstName>
<lastName>Cosby</lastName>
</person>
<person id="4" >
<firstName>Albert</firstName>
<lastName>Einestein</lastName>
</person>
<person id="5" >
<firstName>George Bernard</firstName>
<lastName>Shaw</lastName>
</person>
</persons>
我想在每次点击按钮时将所选ID的(删除)(名字+姓氏)添加到(从)我的列表中。 换句话说,我想添加例如(名字+姓氏)id = 4 然后使用其他按钮(如删除按钮)我想从列表中删除它。
我使用了数据提供程序,但问题是它添加了整个xml类而不是我选择的类元素。 什么解决方案?
答案 0 :(得分:0)
你的问题对我来说不是很清楚。以下是我从你的问题中解决的问题。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private var lastId:int = 5;
private function addNode():void
{
var suffix:int = int(Math.random()*100000);
lastId++;
var node:String = '<person id="'+lastId+'" >' +
'<firstName>FirstName'+suffix+'</firstName>' +
'<lastName>LastName'+suffix+'</lastName>' +
'</person>'
var xmllist:XMLList = new XMLList(node);
xmlData.appendChild(xmllist);
trace(xmlData.toXMLString())
txtResult.text = xmlData.toString();
}
private function removeNode():void
{
try{
delete xmlData.person[XMLList(xmlData.person.(@id==txtId.text)).childIndex()];
txtResult.text = xmlData.toString();
}catch(err:Error){
Alert.show("person with this id is not found.");
}
}
]]>
</fx:Script>
<fx:Declarations>
<fx:XML id="xmlData" source="data.xml" />
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:VGroup width="100%" height="100%">
<mx:Form>
<mx:FormItem label="Enter Node ID:" >
<mx:TextInput id="txtId" restrict="0-9" />
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button label="Add Node" click="addNode()"/>
<mx:Button label="Remove Node" click="removeNode()"/>
</mx:FormItem>
</mx:Form>
<mx:TextArea id="txtResult" width="100%" height="100%" />
</s:VGroup>
</s:Application>
这里的代码data.xml
是有问题的xml。