2011-02-14 57 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