其实以前就遇到过这个问题,只是这次再次遇到,把最终的解决办法列出来:
问题:
当使用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
分享到:
相关推荐
flex通过httpService读取xml数据到datagird中,通过读取远程URL的XML数据(这里保存到本地),保存到ArrayCollection中,然后在将ArrayCollection绑定到datagird上 环境:flex3.0 可直接运行
Flex加载xml数据三种方式:HTTPService XMLLoader XML类
flex 解析xml文件,利用httpservice读取xml文件,按要求过滤字符串,获取对应的值
Flex 使用HttpService组件发送XML数据与java交互 例子+文档
Flex中利用HTTPService MXML标签(tag)将XML转化为对象(objects)
flex4 httpservice 例子 提取xml数据
1、HttpService.dll是一个用于...注:编程引用链接库时,请将"HttpService.dll"和"HttpService.XML"一起放到同一个目录下,这样在使用过程中就会有相关的注释说明,这样就不会造成看着函数而没有使用说明而一头雾水。
FLEX与JSP结合并测试HTTPService与java用XML通信
httpservice post get传数据
用httpservice和urlloader方法读xml里的数据
一个flex 与 java 通过HTTPService 通信的小例子,希望对flex新手有点帮助
FLEX与JSP结合并测试HTTPService与java用XML通信.doc
简单的 flex HttpService例子,可供初学者参考
过年期间抽空在弄flex前端对象与后端java对象利用httpservice交互的问题。比如前端需要user list数据,构造了flex user object类。
.net 下用asp.net实现的HttpService例子; HttpService的部署方式跟网站的部署方式一样; 运行HttpClient前注意先配置下配置文件,Url为HttpService的地址
使用httpService通过xml传递对象的时候,用于flex端解析xml的实例。
flex 和java的通信使用的是httpservice 但当时遇到了不知道如何获取java端的返回值给flex前端 我这个就是为了解决你烦恼而来的 绝对不忽悠你