0

我想将数据从我的 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 类而不是我选择的类元素。解决方案是什么?

4

1 回答 1

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。

于 2013-07-18T05:26:45.790 回答