显示阵列集合中的XML节点

时间:2010-07-14 08:27:48

标签: flex adobe flex4 flash-builder

我已经加载了一个XML Doc。我创建了一个Horizo​​ntal List并将arraycollection引用为Data Provider。但我现在需要做的就是从中获取数据。

我有3个节点/变量。它们是id,title,thumbnail。

但当我将数据拉出时:{videos.title} Flex Builder给我错误 - “访问未定义的属性视频”

现在我知道它存在,因为当我将dataProvider设置为{videos}时,它会毫无问题地提取数据。

我的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="800" minHeight="600" 
           initialize="channelList.send(),videoList.send()" 
           pageTitle="Video List" 
           width="100%" height="100%" backgroundColor="0x0000" xmlns:local="*">

<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";
    @namespace mx "library://ns.adobe.com/flex/mx";
</fx:Style>

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.messaging.Channel;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        import spark.skins.spark.DefaultComplexItemRenderer;
        import spark.skins.spark.DefaultItemRenderer;

        import videoObjects.Videoinfo;

        // Set the Videos XML to an Array Collection

        var videos:ArrayCollection = new ArrayCollection();


        // Event Handler

        protected function  videoRetrieval_resultHandler(event:ResultEvent):void {

            var videoData:ArrayCollection = event.result.videos.video;

            var viddata:Videoinfo;

             for each (var vid:Object in videoData)
             {
                 viddata = new Videoinfo();

                 viddata.id = vid.id;
                 viddata.title = vid.title;
                 viddata.thumbnail = vid.thumbnail;
                 videos.addItem(viddata);
             }
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->

    <s:HTTPService id="channelList" 
                   url="http://www.spriing.dev/videolist/channelinfo.php" 
                   showBusyCursor="true">
    </s:HTTPService>

    <s:HTTPService id="videoList" 
                   url="http://www.spriing.dev/videolist/videolist.php" 
                   showBusyCursor="true" 
                   result="videoRetrieval_resultHandler(event)">
    </s:HTTPService>

</fx:Declarations>

    <!-- Set Background Image -->
        <mx:Image source="{channelList.lastResult.channels.channel.background_image}" width="100%" height="100%" />
    <!-- End Background Image -->

    <!-- Top Nav / Site Logo etc.. -->
    <s:Group>
        <mx:Image source="file:/Users/stuartblackett/Sites/videolist/img/pokerstars.png" x="14" y="9" />
    </s:Group>

    <!-- Group Channel Information -->
    <s:Group width="100%" height="100%" x="10" y="10" styleName="channelInfo">
        <s:Label text="{channelList.lastResult.channels.channel.name}"  x="19" y="77" width="331" color="#FFFFFF" fontSize="14"/>
        <s:Label text="{channelList.lastResult.channels.channel.description}"  x="19" y="106" width="331" color="#FFFFFF" height="70"/>
        <s:Label text="{channelList.lastResult.channels.channel.breadcrumb}"  x="20" y="61" color="#FFFFFF"/>
        <mx:Image source="{channelList.lastResult.channels.channel.logo}" x="199" y="78" />

            <s:Group width="100%" height="100%">
                <!-- Group Video Data -->

                <s:Label text="FULL EPISODES" color="#FFFFFF" x="493" y="501" height="21"/>
                <mx:HorizontalList id="videoArea"
                                   rowHeight="160" 
                                   columnWidth="180" 
                                   columnCount="5" 
                                   x="489" y="527" 
                                   dataProvider="{videos}" 
                                   labelField="title"
                                   width="653">
                        <mx:itemRenderer>
                            <fx:Component>
                                <mx:VBox>
                                    <mx:Image source="img/"/>
                                    <mx:Label text="{videos.title}"/>
                                </mx:VBox>
                            </fx:Component>
                        </mx:itemRenderer>
                    </mx:HorizontalList>

                <s:VideoPlayer y="63" width="649" height="415" x="493"/>

            </s:Group>

    </s:Group>

 </s:Application>

如何获取XML节点:标题,当然还有缩略图?

1 个答案:

答案 0 :(得分:1)

<mx:Label text="{videos.title}"/>更改为

<mx:Label text="{data.title}"/>

要显示图像,请将其设为

<mx:Image source="img/{data.thumbnail}"/>

您尝试从插入式项呈示器(videos标记内)访问<mx:component> - 您无法直接执行此操作。项呈示器可以通过outerDocument属性访问其外部文档的属性。因此outerDocument.videos.title会起作用 - 或不起作用。在这种情况下,它将无法按您希望的方式工作 - 您希望列表中的每个项目都显示相应的标题。 outerDocument.videos.title只会为您提供标题的XML列表 - 使用data属性来访问当前项目。