• xml与Flex


    一、最简单模式:Flex通过httpservice和dataProvider进行数据传输

    Xml格式如下:
    <?xml version="1.0" encoding="utf-8" ?>
    <Result>
    <NodeA>value1</NodeA>
    <NodeB>valve2</NodeB>
    ……………………………………………
    ……………………………………………
    </Result>
     
    在Flex中有一个DataGrid,这样就可以用httpservice来使DataGrid获取xml的数据了。
    Flex端如下:
    <?xml version="1.0" encoding="utf-8"?>   
    <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
        title="Blog Details">
        <mx:HTTPService showBusyCursor="true" url="http://localhost:8080/text.xml%22/ id=”xmlRecord”>
    <mx:DataGrid width="712" height="338" dataProvider="{xmlRecord.lastResult.Result}">
            <mx:columns>
                   <mx:DataGridColumn headerText="SectionA" dataField=" NodeA" width="400" />
                   <mx:DataGridColumn headerText="SectionB" dataField=" NodeB" width="30"/>
          。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
            </mx:columns>
    </mx:DataGrid>
    </mx:TitleWindow>
     
    ※ 其中红字部分必须一致,由于这个sample比较简单,所以Kenshin就不深入研究了。
    蓝字部分一定要按照xml的结构书写。
    这样在run 这个Flex的时候,在DataGrid中就会显示NodeA&NodeB的内容即value1&value2

    二、稍微复杂一些模式
     
    Xml端代码如下:
    <?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>value1</title>
        <link>http://www.k-zone.cn/zblog</link>
        <description>关注Flex,Java,DotNet,Web Desgin</description>
    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    </channel>
    </rss>

    Flex端如下:
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="absolute"
        creationComplete="feedRequest.send()" >

        <mx:HTTPService
            id="feedRequest"
            url=" http://localhost:8080/text.xml"
            useProxy="false" />

        <mx:Panel x="10" y="10" width="475" height="400" layout="absolute"
            title="{feedRequest.lastResult.rss.channel.title}">
        </mx:Panel>
        。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    </mx:Application>
    如果想要得到上面的xml中title的值,只需按照蓝字部分书写即可得到Title的value。
    以此类推如果想要得到xml中link的内容,应该按如下书写
    feedRequest.lastResult.rss.channel.link

    三、复杂一些的模式,即xml的格式比较特殊

    Xml端代码如下:
    <?xml version="1.0" encoding="utf-8"?>
    <Result>
    <SectionA SubSectionA1=”value1” SubSectionA2=”value2”/>
    <SectionB SubSectionB1=”value3” SubSectionB2=”value4”/>
    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    </Result>

    如果大家想要得到SubSectionA1的内容,就显得无从下手了。其实用E4X在actionscript3中已经支持的非常完美了。并且Flex builder2提供的help也有这样的例子。但是我找了一下,没有这样的例子,所以自己研究了一下。并且把结果告诉大家。
    要显示SubSectionA1的内容。Flex端如下书写:

    var loader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest("data/sampleData.xml");
    loader.load(request);
    loader.addEventListener(Event.COMPLETE, onComplete);
            }

    public function onComplete(event:Event):void {
        var externalXML:XML;
        var loader:URLLoader = URLLoader(event.target);
        externalXML = new XML(loader.data);
        trace(externalXML. SectionA [0].@ SubSectionA1);
    }

    其中红字部分就是SubSectionA1的内容了。其中externalXML. SectionA [0].@ SubSectionA1中的[0]是xml的record的条数。@后面的就是想要得到的子节点的值。其内容可以是SubSectionA2、SubSectionB等等的值。

    关于xml的格式在继续引伸一下。以下的格式
    <?xml version="1.0" encoding="utf-8"?>
    <Result>
    <SectionA SubSectionA1=”value1”/>
    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    </Result>
    相当于如下的格式:
    <?xml version="1.0" encoding="utf-8"?>
    <Result>
    <SectionA>
    <SubSectionA1>value1”</ SubSectionA1>
    </SectionA>
    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    </Result>

    以上就是我关于XML和Flex进行通讯的一些想法,尤其是第三种格式,尤其重要。
  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/OwenWu/p/1689801.html
Copyright © 2020-2023  润新知