2010-05-03 14:40

[ActionScript] 讀取 XML 資料格式

1.資料欄位以子節點方式載入與解析

<!-- load_1.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<xmltest>
<parentnode>
<id>1</id>
<name>小明</name>
<date>2010-10-10</date>
</parentnode>
<parentnode>
<id>2</id>
<name>小王</name>
<date>2010-5-15</date>
</parentnode>
</xmltest>


/*建立 XML()*/
LoadXml_1 = new XML();

/*捨棄只包含空白字元的文字節點*/
LoadXml_1.ignoreWhite=true;

/*載入完成時所執行的程式*/
LoadXml_1.onLoad = function(){
articleList = this.firstChild.childNodes;
var txt = "";
for (i=0; i<articleList.length; i++) {
elementList = articleList[i].childNodes;
for (j=0; j<elementList.length; j++) {
elementTag = elementList[j];
switch(elementTag.nodeName){
case "id":
txt += elementTag.firstChild.nodeValue+',';
break;
case "name":
txt += elementTag.firstChild.nodeValue+',';
break;
case "date":
txt += elementTag.firstChild.nodeValue+',';
break;
}
}
txt += '\n';
}

trace(txt); /*顯示載入的資料*/
}

/*開始載入 load_1.xml*/
LoadXml_1.load("load_1.xml");



2.資料欄位以屬性方式載入與解析

<!-- load_2.xml -->
<?xml version="1.0" encoding="utf-8"?>
<xmltest>
<parentnode id="1" name="小明" date="2010-10-10" />
<parentnode id="2" name="小王" date="2010-5-15" />
</xmltest>


/*建立 XML()*/
LoadXml_2 = new XML();

/*捨棄只包含空白字元的文字節點*/
LoadXml_2.ignoreWhite=true;

/*載入完成時所執行的程式*/
LoadXml_2.onLoad = function(success) {
var articleList = this.firstChild.childNodes;
var txt = "";
for (i=0; i<articleList.length; i++){
txt += articleList[i].attributes.id+','
+articleList[i].attributes.name+','
+articleList[i].attributes.call+','
+articleList[i].attributes.channel+"\n";
}

trace(txt);/*顯示載入的資料*/
};

/*開始載入 load_2.xml*/
LoadXml_2.load("load_2.xml");




參考資料:
Flash CS4 Professional ActionScript 2.0 - XML

0 回應: