如何加载,编辑和保存XML?

时间:2013-06-07 19:11:14

标签: xml actionscript-3 actionscript air

我想将一些XML数据加载到文本字段中;我将编辑数据,之后,我想点击一个按钮,数据将以XML格式保存。

以下是截图:

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

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

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

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

<?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: Working with XML