This page was last modified 10:24, 6 June 2008.
XMLファイルをロード・解析する方法
From Forum Nokia Wiki
原文(英語): How to load and parse XML files
対象となるバージョン: Flash Lite 2.x
本アプリケーションは、N95(Flash Lite 2.0)上でネットワーク接続を試みると失敗します。
ここでは、基本的なXMLドキュメントを使用します。
<myPhone> <modelName>Nokia N73</modelName> <screen> <height>240</height> <width>320</width> </screen> <cpu> <type>ARM 9</type> <clock_rate>220</clock_rate> </cpu> </myPhone>
Contents |
XMLファイルをロードする
下記スクリプトでは、XMLファイルをロードします。
// create a new XML object var myPhoneXML = new XML(); //Text nodes that contain only white space are discarded myPhoneXML.ignoreWhite = true; // define the onload handler myPhoneXML.onLoad = function(success) { if (success) { trace("XML loaded"); } else { trace("Loading failed"); } }; // load the XML file into the myPhoneXML object myPhoneXML.load("myPhone.xml");
- XMLオブジェクト上のload()メソッドは、リモートホストからデータをダウンロードします。
- データのダウンロードが終わった時、XMLオブジェクト上でonLoadイベントが発動します。
XMLデータのナビゲーション
- firstChild: 親ノードの子リストにおける最初の子ノード
trace(myPhoneXML.firstChild); // Output: <myPhone>[...]</myPhone> trace(myPhoneXML.firstChild.firstChild); // Output: <modelName>[...]</modelName>
- lastChild: 親ノードの子リストにおける最後の子ノード
trace(myPhoneXML.firstChild.lastChild); // Output: <cpu>[...]</cpu>
- nextSibling: 親ノードの子リストにおける、ある子ノードの次のノード
trace(myPhoneXML.firstChild.firstChild.nextSibling); // Output: <screen>[...]</screen>
- previousSibling: 親ノードの子リストにおける、ある子ノードの前のノード
ノード値を取得する
- 下記の例で、<clock_rate>のノード値をチェックします。
trace(myPhoneXML.firstChild.lastChild.lastChild.firstChild.nodeValue); // Output: 220
- XMLNodeオブジェクトを使用して、画面解像度を取得する一例
var screenNode:XMLNode = myPhoneXML.firstChild.firstChild.nextSibling; trace("Resolution: " + screenNode.firstChild.firstChild.nodeValue + " x " + screenNode.lastChild.firstChild.nodeValue); // Output: Resolution: 240 x 320
- 子ノードを一通り繰り返す一例
for (var aNode:XMLNode = myPhoneXML.firstChild.firstChild.nextSibling.firstChild;aNode != null;aNode=aNode.nextSibling) { trace(aNode.firstChild.nodeValue); } // Output: 240 // 320
ダウンロード
例題のソースコードは、下記サイトからダウンロードできます。
このFlash Lite 2.xアプリケーションは、最新のNokia端末5機種についての情報を取得し示します。
また、RSSフィードとFlash LiteのXML操作メソッドを使用しています。
最新のNokia端末5機種 240*320.zip

