ActionScript讀取YAML

摘要:ActionScript讀取YAML

前幾個星期跟學長討論關於 ActionScript 處理 XML 部分的問題,其實 ActionScript 在處理上,還是透過我們熟悉的 XPath 的方式,
不過它在相對的一些搜尋上,也有另外提供很多不錯的做法,例如: length() 或是透過 [ ] 的方式來新增或修改指定的 element 或是 attribute,
一些基本的功能在處理上,仍有些不方便,例如:如果要特別搜尋或讀取某一個特定的值時,就不是這麼好處理了。
因此學長介紹了「Yaml」給我,關於 yaml的說明,我們可以參考 Wikipedia上的說明與範例:
 
What's  YAML:
"YAML Ain't Markup Language" (abbreviated YAML) is a data serialization language designed to be human-friendly and work well
with modern programming languages for common everyday tasks. This specification is both an introduction to the YAML language
and the concepts supporting it and also a complete reference of the information needed to develop applications for processing YAML.
 
引用Wikipedia上的說明是:「YAML的語法和其他高階語言類似,並且可以簡單表達清單、雜湊表,純量等資料形態。[2]它使用空白符號縮排和
大量依賴外觀的特色,特別適合用來表達或編輯資料結構、各種設定檔、傾印除錯內容、文件大綱(例如:許多電子郵件標題格式和YAML非常接
近)。儘管它比較適合用來表達階層式(hierarchical model)的資料結構,不過也有精緻的語法可以表示關聯性(relational model)的資料。[3]由於YAML
使用空白字元和分行來分隔資料,使的他特別適合用grep/Python/Perl/Ruby操作。他最讓人容易上手的特色是巧妙避開各種封閉符號,如:引號、
各種括號等,這些符號在巢狀結構時會變得複雜而難以辨認。」
我個人對於 YAML可以透過排版的方式,就可以讓程式有辦法做正確的讀取,感到覺得非常有趣,而且讀取 YAML的方式,就跟一般使用物件是
相似的,讓整個YAML在使用與可讀上確實增加了不少。另外關於YAML的設計目標,如下:
 
The design goals for YAML are:
  1. YAML is easily readable by humans.
  2. YAML matches the native data structures of agile languages.
  3. YAML data is portable between programming languages.
  4. YAML has a consistent model to support generic tools.
  5. YAML supports one-pass processing.
  6. YAML is expressive and extensible.
  7. YAML is easy to implement and use.
 
以上相關於 YAML的說明,我這邊就不再多做介紹,大家可以參考 http://zh.wikipedia.org/wiki/YAML與"YAML Ain't Markup Language
(YAML™) Version 1.1"這一個標準的白皮書,將會有更多說明的部分。 那麼YAML大致的格式如下:
 
american:  
   - Boston Red Sox   
   - Detroit Tigers   
   - New York Yankees 
national:  
   - New York Mets   
   - Chicago Cubs   
   - Atlanta Braves 
- 
   name: Mark McGwire   
   hr: 65   
   avg: 0.278
- 
   name: Sammy Sosa   
   hr: 63   
   avg: 0.288
 
上方的YAML內容,主要說明:MLB美聯有紅襪、老虎、洋基,國聯有大都會、小熊、勇士,最後是描述Mark McGwire打了65支全壘打,
可以發現只要透過空白來排出一個版面,讓人方便讀取,因此YAML的可讀性比起過去的XML來說,較讓人容易理解,而且不用去撰寫複雜的
WSDL就讓人覺得方便不少。
那麼要在ActionScript 中怎麼讀取 YAML呢?在介紹之前我去搜尋了相關的資訊,並且找到有人寫好的一些 SWC可以提供給我們來讀取 YAML,
http://code.google.com/p/as3yaml/ 在這個檔案包括做好的SWC,你只需把他匯入到你的 Flex Builder 就可以來輕鬆的讀取 YAML。
我引用http://flexonrails.net/?p=98上所製作的範例來加以介紹:把 YAML的內容轉換成ActionScript的物件
 
---With the following YAML stored in a file called myYaml.yaml
Date: 2001-11-23 15:03:17 -5 
User: ed 
Fatal: Unknown variable "bar" 
Stack: 
    - file: TopClass.py 
      line: 23 
      code: |
            x = MoreObject("345\n") 
    - file: MoreClass.py 
      line: 58 
      code: |-
            foo = bar
 
---You can load then load the YAML and decode it as follows.
   1: public function loadYaml() : void 
   2: { 
   3:     var loader : URLLoader =  new URLLoader(); 
   4:     loader.load(new URLRequest('myYaml.yaml')); 
   5:     loader.addEventListener(Event.COMPLETE, onYamlLoad); 
   6: } 
   7: public function onYamlLoad(event : Event) : void 
   8:  
   9:     var yamlMap : HashMap = YAML.decode(event.target.data) as HashMap; // returns a HashMap 
  10:     trace(yamlMap.get("Date"));  // returns a Date object and prints: Fri Nov 23 15:03:17 GMT-0500 2001 
  11:     trace(yamlMap.get("User"));  // returns a String and prints: ed 
  12:     trace(yamlMap.get("Fatal")); // returns a String and prints: Unknown variable "bar" 
  13:     trace(yamlMap.get("Stack")); // returns an Array and prints: [object HashMap],[object HashMap] 
  14:     trace(yamlMap.get("Stack")[0].get("line"));  // returns an Int and prints: 23 
  15:     trace(yamlMap.get("Stack")[0].get("code"));  // returns a String and prints: x = MoreObject("345\n")     
 
There are some more examples available in the as3yaml docs. 這一個範例,說明一個 YAML檔內容,我們可以透過在Flex Builder上匯入
as3yaml_binary_swc_041.zip 解壓縮後的 SWC,就可以正確的找到 HashMap的Class來使用。如果你對於YAML使用的Class不清楚的話,
也可以到 http://as3yaml.googlecode.com/svn/trunk/docs/index.html 查詢相關的說明與範例。這個範例算是比較簡單的,希望對不太清楚 YAML的
初學者,可以跟我一樣先看一下這一篇,我想會很快建立起觀念的。
 
* 程式範例下載: 
 
References: