`
shizhi
  • 浏览: 47825 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Flex
XML 
阅读更多
其实以前就遇到过这个问题,只是这次再次遇到,把最终的解决办法列出来:

问题:

当使用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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics