首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

Flex读取XML小结

2012-11-08 
Flex读取XML总结1. Model标签形式首先声明Model标签,mx:Model idmodel sourceconfig.xml/config

Flex读取XML总结

1. Model标签形式

首先声明Model标签,

<mx:Model id="model" source="config.xml"/>

<config>                 <username>Flex</username>                 <password>123456</password>  </config> <config>                <username>Flex</username>               <password>123456</password></config>
?

Flex代码
<mx:Label text="{model.username}"/>?

<mx:Label text="{model.username}"/>

那么,只需要在初始化事件中直接调用即可,例如:model.username, model.password即可取到想要的值。

?

2 URLLoader

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"                 layout="absolute"                 initialize="init()">      <mx:Script>          <![CDATA[              import mx.controls.Alert;               private function init():void              {                  var urlrequest:URLRequest=new URLRequest("test.xml");                  var loader:URLLoader=new URLLoader();                  loader.load(urlrequest);                  loader.addEventListener(Event.COMPLETE, completehandler);              }               private function completehandler(event:Event):void              {                  var xml:XML=new XML(event.target.data);                  Alert.show(xml.toString());              }          ]]>      </mx:Script>  </mx:Application> 

?3? FileStream

private function readXML1():void{     var xmlFile:File = new File("file path");     var stream:FileStream = new FileStream();     stream.open(xmlFile, FileMode.READ);     var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));     stream.close();}Flex写XML的方法:public static function writeXMLFile(xml:XML):void{     var stream:FileStream = new FileStream();     var xmlFile:File = new File("file path");     var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';     outputString += xml.toXMLString();     outputString = outputString.replace(/\n/g, File.lineEnding);     stream = new FileStream();     stream.open(xmlFile, FileMode.WRITE);     stream.writeUTFBytes(outputString);     stream.close();}?

?可以通过WebService,HttpService以及RemoteObject等方式调用Java或C#等完成文件的读写操作。

Flex端:

<mx:Script>    javaService.getOutStream(xml);</mx:Script><mx:RemoteObject id="javaService" destination="fileOutput" showBusyCursor="true"result="Alert.show('设置成功,请刷新浏览器!')" fault="Alert.show('文件输出错误!')"/>

?Java端:

package util;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStreamWriter;public class setView {public boolean getOutStream(String str) {   try {    File f = new File("webapps/test/xml/example.xml");    FileOutputStream fos = new FileOutputStream(f, false);    OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");    BufferedWriter bw = new BufferedWriter(osw);    bw.write(str);    bw.flush();    bw.close();    osw.close();    fos.close();    return true;   } catch (Exception e) {    e.printStackTrace();    return false;   }}}

?config.xml添加配置

<destination id="fileOutput" channels="my-amf">        <properties>            <source>util.setView</source>        </properties>    </destination>
?

热点排行