HTTPService读取XML时,当节点为1时的问题解决

本文详细介绍了在使用ActionScript3的HTTPService读取XML文件时,针对XML节点数量变化导致的类型从ArrayCollection到ObjectProxy的转换问题,并提供了解决方案。

 转载地址:http://leopowerword.javaeye.com/blog/341748

 

其实以前就遇到过这个问题,只是这次再次遇到,把最终的解决办法列出来: 

问题: 

当使用HTTPService读取XML文件时,存在多个XML节点时,其类型为ArrayCollection,但当节点为1时,其类型不是ArrayCollection而是ObjectProxy了 

拿一个实际的XML举例,我需要提取其中的Question节点以生成相应投票题目。 

情形一:存在多个Question(两个或两个以上)节点时,直接可以将其作为ArrayCollection来使用,没有任何问题; 

情形二:当仅存在一个Question节点时,若直接当成ArrayCollection来处理便会出错,通过Debug可以发现,这时的类型为ObjectProxy(关于ObjectProxy类型的含义可以查看参考手册),这时候就需要额外处理了。 



情形一XML: 

<Vote State="ok"> 
<Survey> 
<SurveyHead> 
    <Title>aaaaaaaaaaaaaaaaaaaaaaaaaaaaa</Title> 
    <Author>1</Author> 
</SurveyHead> 
<SurveyBody> 
<Question> 
    <Describe>asdfasdfadf</Describe> 
    <Type>单选</Type> 
    <Options> 
        <item id="1" Content="asdfasdfasdfasd" Result="0"/> 
        <item id="2" Content="asdfasdfasdf" Result="0"/> 
        <item id="3" Content="dddd" Result="0"/> 
    </Options> 
   <Hot>0</Hot> 
</Question> 

<Question> 
    <Describe>hstgsdfgsdfg</Describe> 
    <Type>多选</Type> 
    <Options> 
        <item id="1" Content="sdhsdgagf" Result="0"/> 
        <item id="2" Content="setesrgsg" Result="0"/> 
        <item id="3" Content="35w3asfag" Result="0"/> 
    </Options> 
   <Hot>0</Hot> 
</Question> 

</SurveyBody> 
</Survey> 
</Vote> 

情形二XML: 

<Vote State="ok"> 
<Survey> 
<SurveyHead> 
    <Title>aaaaaaaaaaaaaaaaaaaaaaaaaaaaa</Title> 
    <Author>1</Author> 
</SurveyHead> 
<SurveyBody> 
<Question> 
    <Describe>asdfasdfadf</Describe> 
    <Type>单选</Type> 
    <Options> 
        <item id="1" Content="asdfasdfasdfasd" Result="0"/> 
        <item id="2" Content="asdfasdfasdf" Result="0"/> 
        <item id="3" Content="dddd" Result="0"/> 
    </Options> 
   <Hot>0</Hot> 
</Question> 
</SurveyBody> 
</Survey> 
</Vote> 



最终找到的一个比较好的解决办法如下: 

var arry:ArrayCollection; 

if(evt.result.Vote.Survey.SurveyBody.Question is ObjectProxy) { 

   arry = new ArrayCollection([evt.result.Vote.Survey.SurveyBody.Question]); 

}else { 
   arry = evt.result.Vote.Survey.SurveyBody.Question as ArrayCollection; 




以后就可以全当成ArrayCollection处理了。 

来源:http://www.jexchen.com/?tag=actionscript3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值