我想问一下如何将变量从 XML 加载到 flash builder。是否可以?我认为我加载了变量,但我的 AS3 已经完成。我的意思是在执行脚本之后加载来自 XML 的变量。知道怎么做吗?感谢您的建议
问问题
934 次
2 回答
0
示例 XML (message.xml)
<?xml version="1.0" encoding="utf-8" ?>
<category>
<movie date = "13/07/31" genre = "Action">
<title>foo</title>
<rate>4.5</rate>
</movie>
</category>
如何将 XML 解析为变量?
试试这个:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class XML_Parsing_Tester extends Sprite
{
private var movieDate:String;
private var movieGenre:String;
private var movieTitle:String;
private var movieRate:Number;
public function XML_Parsing_Tester()
{
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("message.xml"));
loader.addEventListener(Event.COMPLETE, onLoaded);
}
private function onLoaded(e:Event):void
{
var xml:XML = new XML(e.target.data);
movieDate = xml.movie.@date;
movieGenre = xml.movie.@genre;
movieTitle = xml.movie.title;
movieRate = xml.movie.rate;
}
}
}
于 2013-07-20T11:53:15.120 回答
0
假设您将拥有此 xml(此代码完美运行):
<root>
<child id="1">
<detail>
<detailchild description="detail 1 child"/>
<detailchild description="detail 2 child"/>
</detail>
</child>
OpenXml.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com
/flex/spark" firstView="views.OpenXmlHomeView" applicationDPI="160">
</s:ViewNavigatorApplication>
OpenXmlHomeView
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView" viewActivate="view_activateHandler()">
<fx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
public function view_activateHandler():void{
//Open a XML file
var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("../source.xml");
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(request);
}
private function onLoad(e:Event):void
{
var arr:Array = new Array();
var ldr:URLLoader = URLLoader(e.target);
//XML was loaded then read
var myxml:XML = new XML(ldr.data);
var xmlList:XMLListCollection = new XMLListCollection(myxml.children());
//Loop over XML childs to load a Question Array
for each(var obj:Object in xmlList){
arr.push(obj.@id);
var xmlChildren:XMLList = obj.detail;
for each (var qt:Object in new XMLListCollection(xmlChildren.children())){
arr.push(qt.@description);
}
}
lbl.text = arr.toString();
}
]]>
</fx:Script>
<s:Label id="lbl" />
</s:View>
这段代码显示:
1, detail 1 child, detail 2 child
你可以在这里下载这个项目。
注意:这是一个 Flex Mobile 项目。
于 2013-07-20T19:27:31.660 回答