構成
- sample.xml
サンプルのXMLファイル。 - XmlService.as
サンプルのXMLファイルへのアクセスを提供する ActionScript ファイル。 - EmbeddedXmlTest.mxml
上記の利用するサンプルアプリケーション。
各ファイルを sample パッケージ直下に配置すれば動きます。
見た目はこんな感じです。
sample.xml
<words> <word id="1" value="cat" /> <word id="2" value="dog" /> <word id="3" value="cow" /> <word id="4" value="lion" /> <word id="5" value="monkey" /> <word id="6" value="squirrel" /> <word id="7" value="elephant" /> <word id="8" value="hippopotamus" /> <word id="9" value="rabbit" /> </words>
XmlService.as
package sample {
// 埋め込まれた xml ファイルを取得するサービス
public class XmlService {
/**
* 埋め込まれた xml ファイル
*/
[Embed(source="sample.xml")]
private static const _xml:Class;
/**
* xml ファイルの内容を表す XML 型の変数
*/
public static const xml:XML = _xml.data;
/**
* 指定されたインデックスの word 要素を返します。
*/
public static function getWord(index:uint):XML {
return xml.word.(@id==index)[0];
}
}
}
EmbeddedXmlTest.mxml
<?xml version="1.0" encoding="utf-8"?>
<!-- XMLファイルの内容を TextArea と DataGrid に表示するサンプル -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">
<s:layout>
<s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
</s:layout>
<mx:HBox width="200" borderVisible="true" verticalAlign="middle" borderColor="black" borderStyle="solid">
<mx:Label text="id : {idNs.value}"/>
<mx:NumericStepper id="idNs" width="20" change="wordLabel.text = XmlService.getWord(idNs.value).@value" minimum="1" maximum="{XmlService.xml.word.length()}"/>
<mx:Label id="wordLabel" width="100%"/>
</mx:HBox>
<mx:DataGrid id="grid" x="200" y="200" dataProvider="{XmlService.xml.word}" selectedIndex="{idNs.value-1}">
<mx:columns>
<mx:DataGridColumn headerText="id" dataField="@id"/>
<mx:DataGridColumn headerText="value" dataField="@value"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
しかし、この手のネタって検索エンジンで引っ掛けるのが難しいですよね。flex, embed, XML, ActionScript など、どの検索ワードも一般的すぎる、、、。

0 件のコメント:
コメントを投稿