2008年12月13日 星期六

《 AS3 》讀取 XML Object 筆記

紀錄  ActionScript 3.0 Cookbook 的程式碼

將自己覺得重要以及常用的十項寫在下面,看看能不能用這些處理些啥事

1. Using Variables
    var username:String = "Bernie";
    var score:int = 1000;
    var example:XML = <gamescore>
                          <username>{username}</username>
                          <score>{score}</score>
                      <gamescore>

2. Using Variables by String
    var str:String = "<gamescore><username>"+username+"</username><score>"+score+"</score><gamescore>"
    var example:XML = new XML(str);

3. Adding Elements to an XML Object
    var example:XML = <example/>;
    example.two = " ";
    example = example.insertChildBefore(example.two, <one/>);
    example = example.insertChildAfter(example.two, <three/>);
    trace(example);

4.Adding Text Nodes to an XML Object
    var example:XML = <example/>;
    // <firstname>Bernie</firstname>
    example.firstname = "Bernie";
    // <test>A,B,C</test>
    example.test = ["A", "B", "C"];

5. Adding Attributes to an XML Element (@ operator)
    var example:XML = <example><someElement/></example>;
    example.someElement.@number = 12.3;
    example.someElement.@string = "example";
    /*
    <example>
        <someelement number="12.3" string="example"/>
    </example>
    */
    trace(example);

##CONTINUE##

6. Reading Elements in an XML Tree
    ar menu:XML = //....pass!
    
    // 多層的Tag(在此為label名稱之Tag)屬性質
    function walk(node:XML):void
    {
        for each(var element:XML in node.elements())
        {
            trace(element.@label);
            walk(element);
        }
    }

7. Finding Elements by Name
    items.items[0].name; // 'name'為Tag名稱
    items.items[1].name;
    book.title.toString(); // 'title'為Tag名稱

8.Removing Elements, Text Nodes, Attributes-1
    var example:XML = 
        <example>
            <fruit color="Red">A</fruit>
            <vegetable color="Green">B</vegetable>
            <dairy color="white">C</dairy>
        </example>;
    delete example.fruit.@color;
    delete example.dairy;
    delete example.vegetable.text()[0];
    /*
    <example>
        <vegetable/>
    </example>
    */
    trace(example);

9.Removing Elements, Text Nodes, Attributes-2
    var example:XML = 
        <example>
            <fruit color="Red" name=Apple"/>
        </example>;
    var attributes:XMLList = example.fruit.@*;
    for(var i:int = attributes.length()-1;i>=0;i--)
    {
        delete attributes[i];
    }
    /*
    <example>
        <fruit/>
    </example>
    */
    trace(example);

10. Loading XML
var loader:URLLoader = new URLLoader();
    loader.dadaFormat = DataFormat.TEXT;
    loader.addEventListener(Event.COMPLETE,handleComplete);
    loader.load(new URLReguest("example.xml"));

    private function handleComplete(event:Event):void
    {
        try{
            var example:XML = new XML(event.target.data);
            trace(example);
        }catch(e:TypeError){
            trace("Couldn't ....");
            trace(e.message);
        }
    }

也好久沒有更新文章了,除了忙推甄之外,專題亦是不可開銷啊!
加緊唸書比較實在,而且唸書或者是實做才會有心得可以放上來呀。

沒有留言:

張貼留言