XML info flash template

Hi,
I’m trying to get a flash template to retrieve data from an external xml file. But I do not make it work.
I think the problem is in the class AS file. Because it works when I put the code on frame one.
Can someone have a look and help me?

AS

package
{
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	
	public class databas_nya extends CasparTemplate 
	{
		private var myXMLLoader:URLLoader;
		private var myTimer:Timer; 
		
		private function timerListener (e:TimerEvent):void
		{
			myXMLLoader = new URLLoader();
			myXMLLoader.load(new URLRequest("databas.xml"));
			myXMLLoader.addEventListener(Event.COMPLETE, processXML);
		}
		
		private function processXML (e:Event):void
		{
			var myXML:XML = new XML(e.target.data);
			f0.text=myXML.namn;
			f1.text=myXML.title;
		}
	}
	
	
}

Here is the link to the files:
https://drive.google.com/drive/folders/1ABXdGIxYm8DVq2LNpz2im16RmsXkDKKE?usp=sharing

Thanks in advance,
//Patrik

There are a few functions that should be overwritten. In the old doc (before GitHub) there was a description of this, but I did not find it any more. At least you should add a postInitialize function:

override public function postInitialize():void {
   //code here
}

Inside this you should initialize and start the timer, as this is called by the TemplateHost after the template is loaded and before anything else.

Okay thanks for your reply, I think I understand what you mean and I have tried adding “postInitialize (): void” but it does not work either.

Here is the code:

package  {
	
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	
	public class databas_nya extends CasparTemplate {
		
	override public function postInitialize():void
   {
	var myXMLLoader:URLLoader = new URLLoader();
	myXMLLoader.load(new URLRequest("databas.xml"));
	myXMLLoader.addEventListener(Event.COMPLETE, processXML);

	function processXML (e:Event):void
	{
		var myXML:XML = new XML(e.target.data);

		f0.text = myXML.namn;
		f1.text = myXML.title;
		
		}
		}
	}
}

Can you see what I did wrong?

var myXMLLoader:URLLoader = new URLLoader();

must be defined outside the postInitialze function, because otherwise it only lives inside of this function.

1 Like

Thank you so much for trying to help me.
I’m not getting the code to work. I have no idea what I’m doing wrong and I’ve tested your idea of postInitialze function.

If you get the time and can check the files I have attached I would appreciate it. But again thanks for all the help.

I got some time to test it and got it working.
Complete folder is here.
https://drive.google.com/file/d/1HhJ7-g8gSMoRg-KN7OlPLSQwp1n_dNMN/view?usp=sharing

as3 code

package 
{
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;

	import se.svt.caspar.template.CasparTemplate;

	public class xmldata extends CasparTemplate
	{
		
		var urlLoader:URLLoader = new URLLoader();
		var urlRequest:URLRequest = new URLRequest("file:///C:/casparcg/xmldata/databas.xml");
		var myTimer:Timer = new Timer(1000);

		public function xmldata()
		{
			urlLoader.addEventListener(Event.COMPLETE, processXML);
			myTimer.addEventListener(TimerEvent.TIMER ,timerListener);
			myTimer.start();
		}
		function timerListener(e:TimerEvent):void
		{
			urlLoader.load(urlRequest);
		}


		function processXML(e:Event):void
		{
			var myXML:XML = new XML(e.target.data);
			xf0.text = myXML.namn;
			xf1.text = myXML.title;
		}
	}


}
1 Like

Thank you so much for changing the code for me.
Now it works and I have tested the template on my server.
This really helped me.
Thanks again!

1 Like
Privacy Policy   Terms of Service