1

我想将一些 XML 数据加载到文本字段中;我将编辑数据,然后,我想单击一个按钮,数据将保存为 XML。

这是一个屏幕截图:

我的 XML 数据很简单;例如:

<?xml version="1.0" encoding="utf-8"?>
<information>
    <name>Lobby</name>
    <location>1st floor</location> 
</information>

首先,如果我选择一个组合框列表,在文本字段中,将加载 XML 数据,我可以在文本字段中编辑 XML 数据。然后当我单击 时Update,XML 数据将被更改并保存。

我怎样才能做到这一点?

4

1 回答 1

0

遍历 XMLList 项目可能有点棘手,我建议ArrayCollection使用 for in 循环(请参阅附加的代码示例)填充下拉选项,然后在更改事件上使用事件侦听器来解析和填充文本字段。

<?xml version="1.0" encoding="utf-8"?>
 <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" creationComplete="init()"
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        import spark.events.IndexChangeEvent;

        [Bindable]private var ddList:ArrayCollection = new ArrayCollection();
        [Bindable]private var output:String = "";
        private var xbuilding:XMLList = new XMLList(
            <building>
                <room>
                    <name>Lobby</name>
                    <location>Some Place</location>            
                </room>
                <room>
                    <name>Another Room</name>
                    <location>Some Other Place</location>              
                </room>
                <room>
                    <name>Yet Another Room</name>
                    <location>The Final Place</location>               
                </room>
            </building>);

        protected function init():void
        {
            for each (var roomName:String in xbuilding.room.name){
                ddList.addItem(roomName);
            }
        }

        protected function dropDown_changeHandler(event:IndexChangeEvent):void
        {
            output = "name :"+xbuilding.room[event.newIndex].name+"\nlocation: "+xbuilding.room[event.newIndex].location;

        }

    ]]>
</fx:Script>
<s:VGroup>
    <s:DropDownList dataProvider="{ddList}" change="dropDown_changeHandler(event)"/>
    <s:TextArea text="{output}" editable="false"/>
</s:VGroup>
</s:WindowedApplication>

要编辑或更改 XML 数据,只需将其视为对象数组,确保正确解析输入

xbuilding.room[selectedIndex] = new XMLList(newData)

有关更多信息,我推荐AS3Docs:使用 XML

于 2013-06-10T14:46:16.780 回答