2011-02-14 63 views
0

我想閱讀使用ActionScript 2.0ActionScript 2.0中讀取XML文件

一個XML文件中的問題是,我只能讓我的代碼通過引用它們的位置讀取標籤的價值。

例如 var currentIcon = myXML.firstChild.firstChild.childNodes[0].firstChild;

如何從XMl文件引用標籤名稱檢索值?

例如 var currentIcon = myXML.firstChild.firstChild.childNodes['icon'].firstChild;

這是我的XML文件...

<weather> 
    <current> 
     <icon>Partly cloudy</icon> 
     <temphigh>33</temphigh> 
     <templow>24</templow> 
     <humhigh>55</humhigh> 
     <humlow>95</humlow> 
    </current> 
    <forecast1> 
     <day>Monday</day> 
     <icon>rss_ts2</icon> 
     <temphigh>33</temphigh> 
     <templow>24</templow> 
    </forecast1> 
    <forecast2> 
     <day>Tuesday</day> 
     <icon>rss_ts2</icon> 
     <temphigh>32</temphigh> 
     <templow>23</templow> 
    </forecast2> 
    <forecast3> 
     <day>Wednesday</day> 
     <icon>rss_ts2</icon> 
     <temphigh>32</temphigh> 
     <templow>23</templow> 
    </forecast3> 
</weather> 

回答

1

在AS2你不能直接通過名稱從childNodes陣列檢索節點(可以用E4X和點符號做在AS3) 。你應該使用XML2Object解析器像這樣將您的裝載XML到本地AS2對象:

http://www.sephiroth.it/weblog/archives/2004/01/xml2object.php

然後,你將用點符號訪問XMLweather.current.icon
該鏈接包含XML2Object AS2類和一些示例。

此鏈接介紹如何處理在AS2 XML文檔和AS3區別:
http://www.thetechlabs.com/tech-tutorials/xml/working-with-xml-e4x-and-actionscript-3/

+1

也有幾個AS2 XML->的OBJ解析器上http://proto.layer51.com/ – ocodo 2011-02-14 04:09:42