Flex :HTTPService处理XML文件的两种方法, Asp生成Xml的方法,以及Flex定义变量、赋值的运用。。。。
Posted on 2009-10-21 23:41 且行且思 阅读(3093) 评论(0) 编辑 收藏 举报//********************** 定义url及参数为变量,动态传值。。。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
initialize="myHTTPData.send();trace('calling service');">
<mx:Script>
<![CDATA[
var myURL:String="http://www.bpurcell.org/blog/rss.cfm";
var mode:String="short";
var mode2:String="cat";
var catid="14";
]]>
</mx:Script>
<!-- If you set resultFormat to Object it turns the returning data into an object -->
<mx:HTTPService url="{myURL}" id="myHTTPData" method="GET" resultFormat="object">
<mx:request>
<mode>{mode}</mode>
<mode2>{mode2}</mode2>
<catid>{catid}</catid>
</mx:request>
</mx:HTTPService>
<mx:TextArea text="{myHTTPData.result.RDF.channel.title}" width="250" />
<mx:TextArea text="{myHTTPData.result.RDF.item[0].description}"
width="250" height="200" />
<mx:TextArea text="{myHTTPData.result}" width="250" height="500" />
</mx:Application>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
initialize="myHTTPData.send();trace('calling service');">
<mx:Script>
<![CDATA[
var myURL:String="http://www.bpurcell.org/blog/rss.cfm";
var mode:String="short";
var mode2:String="cat";
var catid="14";
]]>
</mx:Script>
<!-- If you set resultFormat to Object it turns the returning data into an object -->
<mx:HTTPService url="{myURL}" id="myHTTPData" method="GET" resultFormat="object">
<mx:request>
<mode>{mode}</mode>
<mode2>{mode2}</mode2>
<catid>{catid}</catid>
</mx:request>
</mx:HTTPService>
<mx:TextArea text="{myHTTPData.result.RDF.channel.title}" width="250" />
<mx:TextArea text="{myHTTPData.result.RDF.item[0].description}"
width="250" height="200" />
<mx:TextArea text="{myHTTPData.result}" width="250" height="500" />
</mx:Application>
方法二:使用XMLLIST输出完整XML数据 输出数据属性为XML
<?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/halo"
width="1024" height="768" backgroundColor="#020202" creationComplete="menus.send()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var roomsXml:XML;
[Bindable]
private var menuInfo:XMLList;
protected function httpservice1_resultHandler(event:ResultEvent):void
{
roomsXml=new XML(event.result);
menuInfo=roomsXml.node;
trace(menuInfo);
/////输出数据为:
<node name="客厅空间" url="space1.xml"/>
<node name="厨房空间" url="space1.xml"/>
<node name="卧室空间" url="space1.xml"/>
<node name="餐厅空间" url="space1.xml"/>
<node name="浴室空间" url="space1.xml"/>
<node name="儿童房" url="space1.xml"/>
<node name="其他空间" url="space1.xml"/>
trace(menuInfo[0].@name);
/////输出数据为:客厅空间
}
]]>
</fx:Script>
<fx:Declarations>
<mx:HTTPService id="menus" resultFormat="xml" url="dataAsset/rooms.xml" result="httpservice1_resultHandler(event)"/> ////重点:要输出XML数据类型一定要设置resultFormat="xml"
</fx:Declarations>
</s:Application>
<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/halo"
width="1024" height="768" backgroundColor="#020202" creationComplete="menus.send()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var roomsXml:XML;
[Bindable]
private var menuInfo:XMLList;
protected function httpservice1_resultHandler(event:ResultEvent):void
{
roomsXml=new XML(event.result);
menuInfo=roomsXml.node;
trace(menuInfo);
/////输出数据为:
<node name="客厅空间" url="space1.xml"/>
<node name="厨房空间" url="space1.xml"/>
<node name="卧室空间" url="space1.xml"/>
<node name="餐厅空间" url="space1.xml"/>
<node name="浴室空间" url="space1.xml"/>
<node name="儿童房" url="space1.xml"/>
<node name="其他空间" url="space1.xml"/>
trace(menuInfo[0].@name);
/////输出数据为:客厅空间
}
]]>
</fx:Script>
<fx:Declarations>
<mx:HTTPService id="menus" resultFormat="xml" url="dataAsset/rooms.xml" result="httpservice1_resultHandler(event)"/> ////重点:要输出XML数据类型一定要设置resultFormat="xml"
</fx:Declarations>
</s:Application>
HTTPService处理XML文件
ConnAsp.mxml
<%
代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml " layout="absolute" creationComplete="productsRequest.send()">
<mx:HTTPService id="productsRequest" url="http://localhost/FlexAsp_1.asp " />
<mx:DataGrid x="20" y="80" id="productGrid" width="400" dataProvider="{productsRequest.lastResult.guestbook.guests}" >
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="Name" />
<mx:DataGridColumn headerText="Message" dataField="Message" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Asp生成Xml的方法
FlexAsp_1.asp
<%
response.ContentType = "text/xml"
response.Charset="utf-8"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("./flash_asp/Database.mdb")
sql="select Name,Message from guestbook"
set rs=Conn.Execute(sql)
rs.MoveFirst()
response.write("<?xml version='1.0' encoding='utf-8'?>")
response.write("<guestbook>")
while (not rs.EOF)
response.write("<guest>")
response.write("<Name>" & rs("Name") & "</Name>")
response.write("<Message>" & rs("Message") & "</Message>")
response.write("</guest>")
rs.MoveNext()
wend
rs.close()
conn.close()
response.write("</guestbook>")
%>
access数据库
数据库名称 database.mdb
表名称:guestbook
字段:ID,Name,Message
xml单独显示正常,Flex里的DataGrid不显示数据,why
答:
dataProvider="{productsRequest.lastResult.guestbook.guest}"