[Flex] flex读取xml格式数据的一个问题

之前曾花过一段时间整过flex读取xml,以为这方面已经是轻车熟路了,没想到今天在上面在了个跟斗。

     先把代码写下:

mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Script>
 <![CDATA[
  import mx.collections.ArrayCollection;
     import mx.controls.Alert;
  
  [Bindable]
  private var picArr:ArrayCollection = new ArrayCollection();
 
  private function showPic():void
  {
   picArr = picRequest.lastResult.questions.choice;
   Alert.show(picArr.getItemAt(1).title);
  }
  
 ]]>
</mx:Script>

 


<mx:HTTPService id="picRequest" url="books.xml" result="showPic()"/>

 <mx:Button x="269" y="253" label="Button" click="picRequest.send()"/>

</mx:Application>

 

books.xml:

<?xml version="1.0" encoding="utf-8"?>
<questions>
      <choice>
            <title>test xml</title>
      </choice>
</questions>

 

运行时却总是报错,错误代码为:

TypeError: Error #1034: 强制转换类型失败:无法将 mx.utils::ObjectProxy@76dff71 转换为 mx.collections.ArrayCollection。

 

把以前的代码拿出来对了半天都找不错,最后发现问题所在:当xml中只有一条<choice>记录时,就会报错。

在原来的基础上又添了一条记录:

<?xml version="1.0" encoding="utf-8"?>
<questions>
      <choice>
            <title>test xml</title>
      </choice>
</questions>

<questions>
      <choice>
            <title>test xml again</title>
      </choice>
</questions>

运行,无错!

跟本问题尚不清楚,高手知道的话请告诉在下,感激不尽!

 

posted on 2009-03-27 17:11  Felix Fang  阅读(1921)  评论(6编辑  收藏  举报

导航