加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex读取xml文件

发布时间:2020-12-15 01:37:06 所属栏目:百科 来源:网络整理
导读:? ? Flex 读取 xml 文件 文章分类 :Web 前端 主要有两个方法: ? ? ? ( 1 )通过 HTTPService ? ( 2 )通过 URLLoader ? ? ? 代码如下: ? ? ? mxml 代码 ? ? ? Java 代码 ?xml version="1.0" encoding="utf-8"? ?? mx:Application xmlns:mx="http://www.a

?

?

Flex读取xml文件

文章分类:Web前端

主要有两个方法:

?

?

?

1)通过HTTPService

?

2)通过URLLoader

?

?

?

代码如下:

?

?

?

mxml代码

?

?

?

Java代码

<?xml version="1.0" encoding="utf-8"?>??

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">??

<mx:Script>??

?? <![CDATA[??

??? import mx.collections.ArrayCollection;??

??? import mx.rpc.events.ResultEvent;??

??? import mx.rpc.http.HTTPService;??

?????

??? public const xmlUrl:String = "config.xml";??

??? [Bindable] private var colors1:ArrayCollection;??

??? [Bindable] private var colors2:XML;??

?????

??? private function init():void{??

???? //方法一:通过HTTPService??

???? var service:HTTPService = new HTTPService();??

???? service.url = xmlUrl;??

???? service.addEventListener(ResultEvent.RESULT,resultHandler);??

???? service.send();??

??????

???? //方法二:通过URLLoader??

???? var request:URLRequest = new URLRequest(xmlUrl);??

???? var loader:URLLoader = new URLLoader(request);??

???? loader.addEventListener(Event.COMPLETE,loaderCompleteHandler);??

??? }??

?????

??? private function resultHandler(event:ResultEvent):void{??

???? colors1 = event.result.colors.color;??

??? }??

?????

??? private function loaderCompleteHandler(event:Event):void{??

???? colors2 = new XML(event.target.data);??

??? }??

?? ]]>??

</mx:Script>??

<mx:List x="100" y="150" dataProvider="{colors1}" labelField="name">??

</mx:List>??

<mx:List x="300" y="150" dataProvider="{colors2.color}" labelField="@name">??

</mx:List>??

</mx:Application>?

?

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

<mx:Script>

?? <![CDATA[

??? import mx.collections.ArrayCollection;

??? import mx.rpc.events.ResultEvent;

??? import mx.rpc.http.HTTPService;

??

??? public const xmlUrl:String = "config.xml";

??? [Bindable] private var colors1:ArrayCollection;

??? [Bindable] private var colors2:XML;

??

??? private function init():void{

???? //方法一:通过HTTPService

???? var service:HTTPService = new HTTPService();

???? service.url = xmlUrl;

???? service.addEventListener(ResultEvent.RESULT,resultHandler);

? ???service.send();

???

???? //方法二:通过URLLoader

???? var request:URLRequest = new URLRequest(xmlUrl);

???? var loader:URLLoader = new URLLoader(request);

???? loader.addEventListener(Event.COMPLETE,loaderCompleteHandler);

??? }

??

??? private function resultHandler(event:ResultEvent):void{

???? colors1 = event.result.colors.color;

??? }

??

??? private function loaderCompleteHandler(event:Event):void{

???? colors2 = new XML(event.target.data);

??? }

?? ]]>

</mx:Script>

<mx:List x="100" y="150" dataProvider="{colors1}" labelField="name">

</mx:List>

<mx:List x="300" y="150" dataProvider="{colors2.color}" labelField="@name">

</mx:List>

</mx:Application>?

?

config.xml文件

?

?

?

Java代码

<colors>??

<color name="LightGrey" value="#D3D3D3"/>??

<color name="Silver" value="#C0C0C0"/>??

<color name="DarkGray" value="#A9A9A9"/>??

<color name="Gray" value="#808080"/>??

<color name="DimGray" value="#696969"/>??

<color name="Black" value="#000000"/>??

</colors>?

?

<colors>

<color name="LightGrey" value="#D3D3D3"/>

<color name="Silver" value="#C0C0C0"/>

<color name="DarkGray" value="#A9A9A9"/>

<color name="Gray" value="#808080"/>

<color name="DimGray" value="#696969"/>

<color name="Black" value="#000000"/>

</colors>

?

实例:

?

?

?

登录

?

?

?

Java代码

<?xml version="1.0" encoding="utf-8"?>??

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">??

??? <mx:Script>??

??????? <![CDATA[??

??????????? import mx.controls.Alert;??

??????????? import mx.rpc.events.ResultEvent;??

??????????? import flash.net.navigateToURL;??

??????????????

??????????? private function goLogin():void{??

??????????????? login.send();??

??????????? }??

??????????? private function resultHandler(event:ResultEvent):void{??

???? ???????????var entry:String = event.result.users.a.toString();??

??????????????? if(entry == 'ok'){??

??????????

??????????????? }else{??

??????????????????? Alert.show('Username or Password is/are wrong !');??

??????????????? }?????????????????

??? ????????}??

??????? ]]>??

??? </mx:Script>??

??? <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="flexlogin.php" result="resultHandler(event)">??

??????? <mx:request xmlns="">??

??????????? <mx:username> {username.text} </mx:username>??

??????????? <mx:userpwd> {userpwd.text} </mx:userpwd>??

??????? </mx:request>??

??? </mx:HTTPService>??

??????

??? <mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">??

??????? <mx:TextInput x="93" y="51" id="username" fontSize="12"/>??

??????? <mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true"/>??

??????? <mx:Button x="73" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>??

??????? <mx:Label x="32" y="53" text="用户名:" fontSize="12"/>??

??????? <mx:Label x="43" y="97" text="密码:" fontSize="12"/>??

??????? <mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2"/>??

??????? <mx:Label x="10" y="10" text="测试用 用户名 User 密码 123456" fontSize="12" width="243"/>??

??? </mx:Panel>??

??????

</mx:Application>?

?

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

?????? <mx:Script>

????????????? <![CDATA[

???????????????????? import mx.controls.Alert;

???????????????????? import mx.rpc.events.ResultEvent;

???????????????????? import flash.net.navigateToURL;

????????????????????

???????????????????? private function goLogin():void{

??????????????????????????? login.send();

???????????????????? }

???????????????????? private function resultHandler(event:ResultEvent):void{

??????????????????????????? var entry:String = event.result.users.a.toString();

??????????????????????????? if(entry == 'ok'){

?????????????

??????????????????????????? }else{

?????????????????????????????????? Alert.show('Username or Password is/are wrong !');

??????????????????????????? }?????????????????????????

???????????????????? }

????????????? ]]>

?????? </mx:Script>

?????? <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="flexlogin.php" result="resultHandler(event)">

????????????? <mx:request xmlns="">

???????????????????? <mx:username> {username.text} </mx:username>

???????????????????? <mx:userpwd> {userpwd.text} </mx:userpwd>

????????????? </mx:request>

?????? </mx:HTTPService>

??????

?????? <mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">

????????????? <mx:TextInput x="93" y="51" id="username" fontSize="12"/>

????????????? <mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true"/>

????????????? <mx:Button x="73" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>

????????????? <mx:Label x="32" y="53" text="用户名:" fontSize="12"/>

????????????? <mx:Label x="43" y="97" text="密码:" fontSize="12"/>

????????????? <mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2"/>

????????????? <mx:Label x="10" y="10" text="测试用 用户名 User 密码 123456" fontSize="12" width="243"/>

?????? </mx:Panel>

??????

</mx:Application>

?

flexlogin.php

?

?

?

Php代码

<?php???????

$return="";??

if(isset($_POST[username]) && isset($_POST[userpwd])){??

?if ("User"==$_POST[username] && "123456"==$_POST[userpwd])??

??? $return="ok";??

?else?

??? $return="error";??

}??

$xml_return = '<users>';??

$xml_return.= '<a>'.$return.'</a>';??

$xml_return.= '</users>';??

echo $xml_return;?

?

<?php????

$return="";

if(isset($_POST[username]) && isset($_POST[userpwd])){

?if ("User"==$_POST[username] && "123456"==$_POST[userpwd])

??? $return="ok";

?else

??? $return="error";

}

$xml_return = '<users>';

$xml_return.= '<a>'.$return.'</a>';

$xml_return.= '</users>';

echo $xml_return;

?

实例:

?

?

?

rss

?

?

?

Java代码

<?xml version="1.0" encoding="utf-8"?>??

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()">??

??? <mx:Script>??

??????? <![CDATA[??

??????????? import flash.net.navigateToURL;??

??????? ]]>??

??? </mx:Script>??

??? <!-- 利用flex获取并显示数据2 -->??

??? <mx:HTTPService id="feedRequest" url="flexXml.xml" useProxy="false"/>??

??????

??? <mx:Panel x="10" y="10" width="475" height="400" layout="absolute" title="{feedRequest.lastResult.rss.channel.title}" >??

??????????

??????? <mx:DataGrid x="10" y="10" id="dgPosts" width="400" dataProvider="{feedRequest.lastResult.rss.channel.item}">??

??????????? <mx:columns>??

??????????????? <mx:DataGridColumn headerText="Posts" dataField="title"/>??

??????????????? <mx:DataGridColumn headerText="Date" dataField="pubDate" width="150"/>??

??????????? </mx:columns>??

??????? </mx:DataGrid>??

??????????

??????? <mx:LinkButton x="10" y="225" label="Read full part" click="navigateToURL(new URLRequest(dgPosts.selectedItem.link));"/>??

??????? <mx:TextArea x="10" y="175" width="400" htmlText="{dgPosts.selectedItem.description}"/>??

??????????

??? </mx:Panel>??

??????

</mx:Application>?

?

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()">

?????? <mx:Script>

????????????? <![CDATA[

???????????????????? import flash.net.navigateToURL;

????????????? ]]>

?????? </mx:Script>

?????? <!-- 利用flex获取并显示数据2 -->

?????? <mx:HTTPService id="feedRequest" url="flexXml.xml" useProxy="false"/>

??????

?????? <mx:Panel x="10" y="10" width="475" height="400" layout="absolute" title="{feedRequest.lastResult.rss.channel.title}" >

?????????????

????????????? <mx:DataGrid x="10" y="10" id="dgPosts" width="400" dataProvider="{feedRequest.lastResult.rss.channel.item}">

???????????????????? <mx:columns>

??????????????????????????? <mx:DataGridColumn headerText="Posts" dataField="title"/>

??????????????????????????? <mx:DataGridColumn headerText="Date" dataField="pubDate" width="150"/>

???????????????????? </mx:columns>

????????????? </mx:DataGrid>

?????????????

????????????? <mx:LinkButton x="10" y="225" label="Read full part" click="navigateToURL(new URLRequest(dgPosts.selectedItem.link));"/>

????????????? <mx:TextArea x="10" y="175" width="400" htmlText="{dgPosts.selectedItem.description}"/>

?????????????

?????? </mx:Panel>

??????

</mx:Application>?

flexXml.xml

?

?

?

Java代码

<?xml version="1.0" encoding="utf-8"?>??

<rss version="2.0">??

??? <!-- 该文件放到tomcatroot目录 -->??

??? <channel>??

??????? <title>你好,flex获取并显示数据</title>??

?

??????? <item>??

??????????? <title>hello world</title>??

??????????? <description> this is a best exanple in flex,you can do it with me,and may you hava a wanderful future </description>??

??????????? <link>http://hi.baidu.com/shiryu963</link>??

??????????? <guid>http://hi.baidu.com/shiryu963</guid>??

??????????? <category>general</category>??

??????????? <pubDate>2009.6.8 12:17:11</pubDate>??

??????? </item>??

?

??????? <item>??

??????????? <title>hello kava</title>??

??????????? <description> best wishs to me,this is a best exanple in flex,and may you hava a wanderful future </description>??

???? ???????<link>http://hi.baidu.com/shiryu963</link>??

??????????? <guid>gech.com</guid>??

??????????? <category>general</category>??

??????????? <pubDate>2009.8.8 6:20:11</pubDate>??

??????? </item>??

?

??????? <item>??

??????????? <title>hello shiryu</title>??

??????????? <description> i hava a best wish to you,and may you hava a wanderful future </description>??

??????????? <link>http://hi.baidu.com/shiryu963</link>??

??????????? <guid>hahahhahhahahah</guid>??

??????????? <category>improtant</category>??

??????????? <pubDate>2009.4.22 6:20:11</pubDate>??

??????? </item>??

??? </channel>??

</rss>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读