在AS3中对XML进行排序

时间:2012-03-14 19:56:31

标签: xml actionscript-3 sorting

我的XML

<?xml version="1.0"?>
<pages>
  <page>
    <ID>8</ID>
    <subject_ID>3</subject_ID>
    <menu_name_eng>some english</menu_name_eng>
    <menu_name_ger>some german</menu_name_ger>
    <menu_name_fre>some french</menu_name_fre>
    <position>2</position>
    <location>1</location>
    <visible>1</visible>
    <content_eng>some more english</content_eng>
    <content_ger>some more german</content_ger>
    <content_fre>some more french</content_fre>
    <big_image></big_image>
    <video_upload></video_upload>
  </page>
  <page>plenty more page tags</page>
</pages>

和(在ReadSubject.as中)我用

将其解析为AS3
public function ParsePages(pagesInput:XML):void {
    var visibleList:XMLList = pagesInput.page.visible;
    for(var i:uint = 0; i < xmlData.page.length(); i++){
        if (xmlData.page[i].visible == "1") {

            inPosition[i] = xmlData.page[i].position;
            inLocation[i] = xmlData.page[i].location;

            menuName[i] = xmlData.page[i]["menu_name_" + setLang];
            content[i] = xmlData.page[i]["content_" + setLang];

            menuNameEng[i] = xmlData.page[i]["menu_name_eng"];
            menuNameGer[i] = xmlData.page[i]["menu_name_ger"];
            menuNameFre[i] = xmlData.page[i]["menu_name_fre"];

            contentEng[i] = xmlData.page[i]["content_eng"];
            contentGer[i] = xmlData.page[i]["content_ger"];
            contentFre[i] = xmlData.page[i]["content_fre"];
        }
    }
}

然后(在Index.as中)将其添加到舞台上

btnHolder4.txt.text = subjects["menuName" + selLang][3]; 
btnHolder4.addChild(picLoaderTodo);
btnHolder4.x = 1470;
btnHolder4.y = 440;
addChild(btnHolder4);
btnHolder4.buttonMode = true;
btnHolder4.mouseChildren = false;

我想要做但却无法理解的是如何使用inPosition控制数据添加到舞台的顺序。 inPosition,inLocation,content等都是数组,但我真的很新,所以我现在很迷茫。

3 个答案:

答案 0 :(得分:0)

只需循环浏览按钮,将按钮的y位置设置为等于按钮的高度乘以位置。

答案 1 :(得分:0)

如果我理解你的问题,要根据每个“页面”节点的“位置”子节点的值对每个“页面”节点进行排序,你可以使用bubble sort算法,如下例所示:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var pages:XML = 
            <pages>
                <page>
                    <position>2</position>
                </page>
                <page>
                    <position>5</position>
                </page>
                <page>
                    <position>1</position>
                </page>
                <page>
                    <position>3</position>
                </page>
                <page>
                    <position>4</position>
                </page>
            </pages>;

            sortPages(pages);

            trace(pages.toXMLString());

        }// end function

        private function sortPages(pages:XML):void
        {
            var sort:Boolean = true;

            while (sort)
            {
                sort = false;

                for (var i:int = 0; i < (pages.page.length()-1); i++)
                {
                    if (int(pages.page[i].position) > int(pages.page[i+1].position))
                    {
                        var node1:XML = new XML(pages.page[i]);
                        var node2:XML = new XML(pages.page[i + 1]);
                        pages.page[i] = node2;
                        pages.page[i + 1] = node1;
                        sort = true;

                    }// end if

                }// end for

            }// end while

        }// end function

    }// end class

}// end package

答案 2 :(得分:0)

sortOn(xml, ["@firstName", "@lastName"]);

public function sortOn(xml:XML, names:*, options:*=0):void
{
    var i1:int;
    var ar:Array=[];
    var list:XMLList=xml.children();
    for (i1=0; i1 < list.length(); i1++)
        ar.push(list[i1]);

    ar.sortOn(names, options);
    for (i1=0; i1 < list.length(); i1++)
    {
        if (list[i1] != ar[i1])
        {
            delete xml.children()[ar[i1].childIndex()];
            xml.insertChildBefore(list[i1], ar[i1]);
            list=xml.children();
        }
    }
}