action script 3 read values from XML file
I have a problem while reading from XML file in action script 3.
This is my XML-file:
<config>
<production>
<app_id>123</app_id>
<server_path>http://someLinktoAccess</server_path>
<assets_server>http://someLinktoAccess</assets_server>
<payment_url_callback>http://someLinktoAccess</payment_url_callback>
</production>
<stage>
<app_id>123</app_id>
<server_path>http://someLinktoAccess</server_path>
<assets_server>http://someLinktoAccess</assets_server>
<payment_url_callback>http://someLinktoAccess</payment_url_callback>
</stage>
<dev>
<app_id>123</app_id>
<server_path>http://someLinktoAccess</server_path>
<assets_server>http://someLinktoAccess</assets_server>
<payment_url_callback>http://someLinktoAccess</payment_url_callback>
</dev>
</config>
I want to access to each key-value pair in this file. So I want to get from here 4 string variables: app_id, server_path, assets_server, payment_url_callback. How can I get them??
Now I'm using such a code:
private function loadXmlConfig():void
{
//load the loading config xml
var xmlLoader:URLLoader = new URLLoader();
var load_config_path:String = "http://dl.dropbox.com/u/28744968/android_vs.xml";
xmlLoader.addEventListener(Event.COMPLETE, xmlConfigLoadingSuccessed开发者_如何学C);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlConfigLoadingFailed);
xmlLoader.load( new URLRequest( load_config_path ) );
}
private function xmlConfigLoadingSuccessed(event:Event):void
{
var load_config:XML = new XML( event.target.data );
trace(load_config.config.dev.app_id.value);
//startup facade
GameFacade.getInstance().startup( StartupCommand, this );
}
The file is loaded with all values, but I can't access any of them.
What mean this:
var library:XML
library.@url
Thank you!
When using XML class you do not reference the root node in your case "config"
// your code
trace(load_config.config.dev.app_id.value);
// correct code
// the toString method should be called automatically
trace(load_config.production.app_id);
And to answer your other question.
var library:XML
library.@url
the @ symbol is used to access attributes.
<library id=123 >
Why are you putting your config file as an xml anyway?
This Type of thing I create a config file/class/Singleton for.
Here is my solution for this, maby it helps anyone later:
private function loadXmlConfig():void
{
//load the loading config xml
var xmlLoader:URLLoader = new URLLoader();
var load_config_path:String = "http://linkToConfigFile";
xmlLoader.addEventListener(Event.COMPLETE, xmlConfigLoadingSuccessed);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlConfigLoadingFailed);
xmlLoader.load( new URLRequest( load_config_path ) );
}
private function xmlConfigLoadingSuccessed(event:Event):void
{
var load_config:XML = new XML( event.target.data );
var listOfProperties:XMLList = load_config.elements("dev");
var listOfElements:XMLList = listOfProperties[0].elements();
this.f_vars = new Object();
for each (var xmlObj:XML in listOfElements)
{
this.f_vars[xmlObj.name()] = xmlObj.valueOf();
trace(this.f_vars[xmlObj.name()]);
}
//startup facade
}
精彩评论