如何从Flex中的配置文件中读取?

时间:2022-06-03 20:22:04

I'm working on a project for school right now and we're trying to get it set up so that it is easily deployable. The webapp portion of it is written entirely in Adobe flex.

我正在为学校开展一个项目,我们正试图将其设置为易于部署。它的webapp部分完全用Adobe flex编写。

However, we need links to certain files / url's within the code which are different on different machines.

但是,我们需要链接到代码中的某些文件/ url,这些文件/ url在不同的机器上是不同的。

For instance, my server might use 8180 as the port while someone else uses 8080. Or one person is using Windows so a filepath would be C:/... while mine would be /home/...

例如,我的服务器可能使用8180作为端口,而其他人使用8080.或者一个人正在使用Windows,因此文件路径将是C:/ ...而我的将是/ home / ...

Is there any way we could put these files into a separate config file and read them dynamically within the mxml files?

有没有什么办法可以将这些文件放到一个单独的配置文件中并在mxml文件中动态读取它们?

It would be really nice if we didn't have to recompile for each individual deployment...

如果我们不必为每个单独的部署重新编译,那将是非常好的...

Thanks in advance!

提前致谢!

5 个解决方案

#1


You can use HTTPService to load an XML file (or any text file) that is in a location relative to the Flex application SWF. Simply execute the HTTPService on application startup, parse the file, and make the data available wherever you need it.

您可以使用HTTPService加载位于相对于Flex应用程序SWF的位置的XML文件(或任何文本文件)。只需在应用程序启动时执行HTTPService,解析文件,并在任何需要的地方提供数据。

#2


Check out appcorelib, the docs will show you how to use a relative URL like from an assets folder:

查看appcorelib,文档将向您展示如何使用资产文件夹中的相对URL:

loadXML("assets/xml/config.xml);

Don't need to worry about crossdomain if the xml and flex app are on same server.

如果xml和flex应用程序位于同一服务器上,则无需担心跨域。

#3


If you have enabled the local-file sandbox, you may be able to use URLLoader to read a local file:

如果已启用本地文件沙箱,则可以使用URLLoader读取本地文件:

http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=05B_Security_176_04.html

However, your SWF must also be local.

但是,您的SWF也必须是本地的。

If you are loading the SWF remotely, you can connect back to the loading server for a list of resources. That should probably be the preferred solution in most cases.

如果要远程加载SWF,则可以连接回加载服务器以获取资源列表。在大多数情况下,这应该是首选解决方案。

#4


You can pass in parameters into a SWF by adding FlashVars to the HTML that it's running from. FLashVars

您可以通过将FlashVars添加到运行它的HTML中来将参数传递到SWF中。 FLASHVARS

#5


I strongly agree with brd6644. You will need a cross domain policy file on the server where the config files reside. Just copy the following XML to a file named "crossdomain.xml" and put it on the server root of the server that contains your config files.

我非常同意brd6644。您将需要配置文件所在的服务器上的跨域策略文件。只需将以下XML复制到名为“crossdomain.xml”的文件中,并将其放在包含配置文件的服务器的服务器根目录下即可。

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy 
  SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="*" />
</cross-domain-policy>

You may not need this if the swf resides on the same server as the config files. Also, as you can see, this cross domain policy allows access from all domains, so if you care a lot about security (may not be important for a school project), read up a bit on them to see how to configure. Here is a good article

如果swf与配置文件位于同一服务器上,则可能不需要此项。此外,正如您所看到的,此跨域策略允许从所有域进行访问,因此如果您非常关心安全性(对于学校项目可能不重要),请阅读它们以了解如何配置。这是一篇好文章

Also, here is some sample HTTPService code:

另外,这里有一些示例HTTPService代码:

private function init():void{
     get_bands_service.url = yeswewillArtistsURL;
     get_bands_service.method = "GET";
     get_bands_service.addEventListener(FaultEvent.FAULT, onServiceFault);
     get_bands_service.requestTimeout = 20;
     get_bands_service.send();
}

<mx:HTTPService id="get_bands_service" result="parseBandsServiceResult();" useProxy="false" />

#1


You can use HTTPService to load an XML file (or any text file) that is in a location relative to the Flex application SWF. Simply execute the HTTPService on application startup, parse the file, and make the data available wherever you need it.

您可以使用HTTPService加载位于相对于Flex应用程序SWF的位置的XML文件(或任何文本文件)。只需在应用程序启动时执行HTTPService,解析文件,并在任何需要的地方提供数据。

#2


Check out appcorelib, the docs will show you how to use a relative URL like from an assets folder:

查看appcorelib,文档将向您展示如何使用资产文件夹中的相对URL:

loadXML("assets/xml/config.xml);

Don't need to worry about crossdomain if the xml and flex app are on same server.

如果xml和flex应用程序位于同一服务器上,则无需担心跨域。

#3


If you have enabled the local-file sandbox, you may be able to use URLLoader to read a local file:

如果已启用本地文件沙箱,则可以使用URLLoader读取本地文件:

http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=05B_Security_176_04.html

However, your SWF must also be local.

但是,您的SWF也必须是本地的。

If you are loading the SWF remotely, you can connect back to the loading server for a list of resources. That should probably be the preferred solution in most cases.

如果要远程加载SWF,则可以连接回加载服务器以获取资源列表。在大多数情况下,这应该是首选解决方案。

#4


You can pass in parameters into a SWF by adding FlashVars to the HTML that it's running from. FLashVars

您可以通过将FlashVars添加到运行它的HTML中来将参数传递到SWF中。 FLASHVARS

#5


I strongly agree with brd6644. You will need a cross domain policy file on the server where the config files reside. Just copy the following XML to a file named "crossdomain.xml" and put it on the server root of the server that contains your config files.

我非常同意brd6644。您将需要配置文件所在的服务器上的跨域策略文件。只需将以下XML复制到名为“crossdomain.xml”的文件中,并将其放在包含配置文件的服务器的服务器根目录下即可。

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy 
  SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="*" />
</cross-domain-policy>

You may not need this if the swf resides on the same server as the config files. Also, as you can see, this cross domain policy allows access from all domains, so if you care a lot about security (may not be important for a school project), read up a bit on them to see how to configure. Here is a good article

如果swf与配置文件位于同一服务器上,则可能不需要此项。此外,正如您所看到的,此跨域策略允许从所有域进行访问,因此如果您非常关心安全性(对于学校项目可能不重要),请阅读它们以了解如何配置。这是一篇好文章

Also, here is some sample HTTPService code:

另外,这里有一些示例HTTPService代码:

private function init():void{
     get_bands_service.url = yeswewillArtistsURL;
     get_bands_service.method = "GET";
     get_bands_service.addEventListener(FaultEvent.FAULT, onServiceFault);
     get_bands_service.requestTimeout = 20;
     get_bands_service.send();
}

<mx:HTTPService id="get_bands_service" result="parseBandsServiceResult();" useProxy="false" />