如何在as3中添加,编辑,删除和保存xml节点

时间:2015-09-28 05:41:12

标签: xml actionscript-3 air

我的xml文件是这样的,

<GALLERY COLUMNS="3" XPOSITION="30" YPOSITION="30" WIDTH="150" HEIGHT="150">
<IMAGE FULL="Tinku.png" Title="Tinku" THUMB="thumbs/thumb1.jpg" Path="file:///storage/emulated/0/Raconteur/Tinku" />
<IMAGE FULL="Kaal.png" Title="Kaal" THUMB="thumbs/thumb2.jpg" Path="file:///storage/emulated/0/Raconteur/Kaal" />
<IMAGE FULL="Painters.png" Title="Little Painters" THUMB="thumbs/thumb3.jpg" Path="file:///storage/emulated/0/Raconteur/Painters" />
</GALLERY>

如何添加如下所示的新节点

<IMAGE FULL="Lost.png" Title="Lost and Found" THUMB="thumbs/thumb4.jpg" Path="file:///storage/emulated/0/Raconteur/Lost" />

我也想知道如何删除特定节点

var myXMLLoader:URLLoader = new URLLoader();
            myXMLLoader.load(new URLRequest("gallery.xml"));
            myXMLLoader.addEventListener(Event.COMPLETE, processXML);

        function processXML(e:Event):void {
            myXML = new XML(e.target.data);
            images = myXML.IMAGE;
            total = images.length();
            myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
            myXMLLoader = null;             
            var imageName:String = images[i].@FULL;
            loader = new Loader();
            loader.load(new URLRequest("images/" + imageName));
        }

add_bt.addEventListener(MouseEvent.CLICK, addnode);
private function addnode(e:MouseEvent):void
        {
            var new_node:XML = <IMAGE FULL="Lost.png" Title="Lost and Found" THUMB="thumbs/thumb4.jpg" Path="file:///storage/emulated/0/Raconteur/Lost" />
            myXML.appendChild(new_node);
            trace(myXML);
        }

提前致谢

1 个答案:

答案 0 :(得分:0)

要向XML对象添加XML节点,可以使用XML.appendChild(),如下所示:

var xml:XML = 
<GALLERY COLUMNS="3" XPOSITION="30" YPOSITION="30" WIDTH="150" HEIGHT="150">
    <IMAGE FULL="Tinku.png" Title="Tinku" THUMB="thumbs/thumb1.jpg" Path="file:///storage/emulated/0/Raconteur/Tinku" />
    <IMAGE FULL="Kaal.png" Title="Kaal" THUMB="thumbs/thumb2.jpg" Path="file:///storage/emulated/0/Raconteur/Kaal" />
    <IMAGE FULL="Painters.png" Title="Little Painters" THUMB="thumbs/thumb3.jpg" Path="file:///storage/emulated/0/Raconteur/Painters" />
</GALLERY>;

var new_node:XML = <IMAGE FULL="Lost.png" Title="Lost and Found" THUMB="thumbs/thumb4.jpg" Path="file:///storage/emulated/0/Raconteur/Lost" />
xml.appendChild(new_node);

trace(xml);

给出:

<GALLERY COLUMNS="3" XPOSITION="30" YPOSITION="30" WIDTH="150" HEIGHT="150">
    <IMAGE FULL="Tinku.png" Title="Tinku" THUMB="thumbs/thumb1.jpg" Path="file:///storage/emulated/0/Raconteur/Tinku"/>
    <IMAGE FULL="Kaal.png" Title="Kaal" THUMB="thumbs/thumb2.jpg" Path="file:///storage/emulated/0/Raconteur/Kaal"/>
    <IMAGE FULL="Painters.png" Title="Little Painters" THUMB="thumbs/thumb3.jpg" Path="file:///storage/emulated/0/Raconteur/Painters"/>
    <IMAGE FULL="Lost.png" Title="Lost and Found" THUMB="thumbs/thumb4.jpg" Path="file:///storage/emulated/0/Raconteur/Lost"/>
</GALLERY>

然后,要删除XML节点,可以使用delete()运算符:

// delete the node with the "Title" attribut is 'Kaal'
delete xml.*.(@Title == 'Kaal')[0];

trace(xml); 

给出:

<GALLERY COLUMNS="3" XPOSITION="30" YPOSITION="30" WIDTH="150" HEIGHT="150">
    <IMAGE FULL="Tinku.png" Title="Tinku" THUMB="thumbs/thumb1.jpg" Path="file:///storage/emulated/0/Raconteur/Tinku"/>
    <IMAGE FULL="Painters.png" Title="Little Painters" THUMB="thumbs/thumb3.jpg" Path="file:///storage/emulated/0/Raconteur/Painters"/>
    <IMAGE FULL="Lost.png" Title="Lost and Found" THUMB="thumbs/thumb4.jpg" Path="file:///storage/emulated/0/Raconteur/Lost"/>
</GALLERY>

有关使用XML的更多详细信息,请查看here

希望可以提供帮助。