2

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);
            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!

2

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using xml-file because my servers can move from one to another freequently, and to avoid recompile my app - I'm using xml file with all configs there and I can modify them easily.
1

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

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.