使用XML、Array、Object對象處理xml雜談——執行個體討論

來源:互聯網
上載者:User
something.xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<questions>
  <question sth="The People's Republic of China..."/>
  <question sth="I need one more stamp..."/>
  <question sth="All the new computers..."/>
</questions>

在Flash中使用XML對象:
var my_xml = new XML();
var idx = 0;
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success) {
  if (success) {
    trace(this.firstChild.childNodes[idx].attributes.sth);
    //訪問question節點的sth屬性。
  }
};
my_xml.load("something.xml");
當需要遍曆訪問xml時,比較遺憾!它沒有length屬性,而Array對象有。我們把它轉成Array:
var my_xml = new XML();
var my_array = new Array();
var xmlLength = 0;
var idx = 0;
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success) {
  if (success) {
    while (this.firstChild.childNodes[xmlLength]) {
      my_array.push(this.firstChild.childNodes[xmlLength]);
      xmlLength++;
    }
    trace(my_array[idx].attributes.sth);
    //此時訪問question節點的sth屬性的方法有所改變。
  }
};
my_xml.load("question.xml");
再看看Object:
var my_xml = new XML();
var my_obj = new Object();
var idx = 0;
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success) {
  if (success) {
    my_obj = this;
    trace(my_obj.firstChild.childNodes[idx].attributes.sth);
    //這裡訪問question節點的sth屬性的方法與使用XML對象完全一致。但這裡的my_obj一旦改變,my_xml也隨之改變,反之亦然。
  }
};
my_xml.load("question.xml");
比較三者,本人推薦使用Array。

ps: 發布設定為Flash Player 6時不支援“
”軟斷行符號符(即某些文字編輯器如sepy的分行符號,在記事本中是一個小黑疙瘩),Flash Player 7及以上版本支援。

當需要遍曆訪問xml時,比較遺憾!它沒有length屬性,而Array對象有

錯了 XML有一個length 屬性

可以通過
Myxml = new XML();
Myxml.onLoad = function(success) {
if (success) {
secxml = myxml.firstChild.childNodes;
l = secxml.length;
trace(........)
.......

}
};

還可以直接輸出它的長度:
trace(this.firstChild.childNodes.length);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.