Select to view content in your preferred language

How to write a config.xml for a Flex application

2923
3
07-25-2011 11:59 AM
ShravanBonagiri
Deactivated User
Hi Folks,

I have an application that is running fine in .mxml files. But, there are few url's that change from time to time. So changing the url's and associated code all the time would be a tedious process.  So, I thought about writing a config.xml file with all the temporary url's there. Please help me with some knowledge articles or links that can help me get started. I appreciate your time and help.

Thanks,
Sam
Tags (2)
0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus
Sam,

You can look at the Flex Viewer to get an idea and borrow code for that:

var configService:HTTPService = new HTTPService();
            configService.url = "your config.xml url";
            configService.resultFormat = "e4x";
            configService.addEventListener(ResultEvent.RESULT, configResult);
            configService.addEventListener(FaultEvent.FAULT, configFault);
            configService.send();

//config result
    private function configResult(event:ResultEvent):void
    {
        try
        {
            configXML = event.result as XML;
            //todo do something with the configXML
        }
        catch (error:Error)
        {
            //todo show some error message
        }
    }

private function configFault(event:mx.rpc.events.FaultEvent):void
{
    //todo show some error message
}
0 Kudos
KenBuja
MVP Esteemed Contributor
This is the way I've set up an application (not in the SFV framework) to read in a configuration file. The application is a framework where I'm showing the data for several different projects and the XML contains all the parameters for each project, including map service and query URLs. I pass in a parameter in the main URL for the specific project:

http://ccma.nos.noaa.gov/explorer/biomapper/biomapper.html?id=JobosBay
http://ccma.nos.noaa.gov/explorer/biomapper/biomapper.html?id=StJohn

This is the code for the Flex application to read in the XML file
private var xmlParameters:XML;    
private var xmlProjectParameters:XMLList;

private function init():void
{
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.addEventListener(Event.COMPLETE,init_onComplete);
    xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,fail);
    xmlLoader.load(new URLRequest("parameters.xml"));
}

private function fail(event:Event)
{
    Alert.show("Fail");
}

private function init_onComplete(event:Event):void
{
    var params:Object;
    
    try
    {
        var loader:URLLoader = URLLoader(event.target);
        xmlParameters = new XML(loader.data);
        
        params = getURLParameters();
        if (params["id"])
        {
            input = params.id
        }
        else
        {
            Alert.show("Parameter search failed");
            return;
        }
    
        
        xmlProjectParameters = xmlParameters.project.(@urlinput==input);
        var bm:IBrowserManager = BrowserManager.getInstance();
        bm.init();
        bm.setTitle(xmlProjectParameters.@id + " BIOMapper");
         serverName = xmlProjectParameters.service;
        projectTitle = xmlProjectParameters.projectname.@name;

        if (!(xmlProjectParameters.datalayers.dive==undefined))
        {
            diveQueryTask.url = serverName + xmlProjectParameters.datalayers.dive.@url + "/MapServer/" + xmlProjectParameters.datalayers.dive.@layer;

etc...

}

private function getURLParameters():Object
{
    var result:URLVariables = new URLVariables();
    
    try
    {
        if (ExternalInterface.available)
        {
            var search:String = ExternalInterface.call("location.search.substring", 1);
            if (search && search.length > 0)
            {
                result.decode(search);
            }
        }
    }
    catch (error:Error)
    {
        Alert.show(error.toString());
    }
    
    return result;
}
And this is the code for the XML file
<?xml version="1.0" encoding="utf-8"?>
<projects>
  <project id="Jobos Bay" urlinput="JobosBay">
    <projectname name ="Jobos Bay, PR" pdftitle="Jobos Bay, PR: Shallow-water Benthic Habitats" url="http://ccma.nos.noaa.gov/ecosystems/coralreef/ceap/"/>
    <service>http://egisws02.nos.noaa.gov/ArcGIS/rest/services/biomapper/Jobos_</service>
    <datalayers>
      <dive layer="0" url="Dynamic" site_id="Site_ID" videoname="Site_ID" assessment="Assessment" photocount="PhotoCount" symbol="Symbol" legendurl="http://ccma.nos.noaa.gov/explorer/biomapper/jobosbay/images/Dives.png" legendheight="53">
      </dive>
    </datalayers>

etc

  </project>
  
  <project id="St. John" urlinput="StJohn">
    <projectname name="St. John, U.S. Virgin Islands" pdftitle="U.S. Virgin Islands (St. John): Shallow- and Moderate-Depth Benthic Habitats" url="http://ccma.nos.noaa.gov/ecosystems/coralreef/benthic/"/>
    <service>http://egisws02.nos.noaa.gov/ArcGIS/rest/services/biomapper/StJ_</service>
    <datalayers>
      <dive layer="13" url="all" site_id="SITE_ID" videoname="VIDEONAME" assessment="ASSESSMENT" photocount="PHOTOCOUNT" symbol="SYMBOL" legendurl="http://ccma.nos.noaa.gov/explorer/biomapper/stjohn/images/Dives.png" legendheight="103">
      </dive>

etc.

  </project>
 
</projects>
0 Kudos
ShravanBonagiri
Deactivated User
Thank you Robert and Ken. I appreciate it.

I was able to write the config.xml file.
0 Kudos