DClick

Lendo arquivos XML Locais


Para ler um arquivo XML local podemos utilizar a seguinte sintaxe:

Actionscript:
  1. // constantes
  2. private static const packageData:String = "xml/";
  3.    
  4. // variaveis
  5. private var myLoader:URLLoader;
  6. private var myXML:XML = new XML();
  7.  
  8. /**
  9. * Carrega arquivo MyXML
  10. *
  11. */
  12. private function loadXML():void
  13. {
  14.     var XML_URL:String = packageData + "MyXML.xml";
  15.     var myXMLURL:URLRequest = new URLRequest(XML_URL);
  16.     myLoader = new URLLoader(myXMLURL);
  17.     myLoader.addEventListener(Event.COMPLETE, loadXMLHandler);
  18. }
  19.  
  20. /**
  21. * Handler do arquivo XML - arquivo carregado
  22. * @param event
  23. *
  24. */
  25. private function loadXMLHandler(event:Event):void
  26. {
  27.     myXML = XML(myLoader.data);
  28. }

Obs.: No exemplo acima o arquivo MyXML.xml estaria na pasta /xml/.

Importante !

Se a aplicação em questão somente irá acessar arquivos locais podemos utilizar o parâmetro de compilação -use-network=false para evitar o erro abaixo:

SecurityError: Error #2148: SWF file file://[swf_path] cannot access local resource file://[xml_path]. Only local-with-filesystem and trusted local SWF files may access local resources.

Por Ricardo Pettine em 15/February/2008 | Comentar | Trackback


No Translations

Adicionar comentário

(requerido)
(requerido, não será publicado)