Posts Tagged ‘ xml

Printing multi line XML while maintaining the formatting in PHP

A co-worker of mine introduced me to this tip for maintaining the formatting of multi line information in PHP; in this case it is XML. It uses heredoc. The syntax is basically three <<< and then the name of the type of variable...

Here is a code snippet of it in use:

<?php
 
echo <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<someNode>
	{$massiveAmountOfXML}
</someNode>
XML;
 
?>

Embed XML in AS3

There are two methods that I have found for for doing this. (Essentially the same thing, just depends on your coding style and how you want to keep things organized.)

Method 1:

  Embed XML (8.9 KiB, 135 hits)

package  {
 
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
 
	public class EmbedXML extends MovieClip {
 
		[Embed(source="my.xml", mimeType="application/octet-stream")] 
		protected const EmbeddedXML:Class;
		private var _xml:XML;
 
		public function EmbedXML():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
			//_xml = XML(new EmbeddedXML());
 
			var contentfile:ByteArray = new EmbeddedXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}
	}
}

Method 2:

  Embed XML Class (8.9 KiB, 103 hits)

//.as file that you embed the xml in
package {
	import flash.utils.ByteArray;
 
	[Embed(source="my.xml",mimeType="application/octet-stream")]
 
	public class MyXML extends ByteArray {
 
		public function MyXML():void {	}
	}
}
//usage:
package  {
 
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
 
	public class EmbedXMLTwo extends MovieClip {
 
		private var _xml:XML;
 
		public function EmbedXMLTwo():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
			//var myxml:MyXML = new MyXML();
			//_xml = XML (myxml);
 
			var contentfile:ByteArray = new MyXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}	
	}
}

Load XML with LoaderMax

I did have my own set of classes that I use for loading files(images/xml etc) but since I use Greensock for tweening I figured I should use it for loading my assets as well.

//import the classes, u dont need easing for this but you should be using it
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
 
//above your class constructor function
private var _loaderMax:LoaderMax;
private var _xml:XML;	
 
//in your code where you want to use it
private function loadXML():void {
_loaderMax = new LoaderMax({name:"mainQueue", onComplete:completeHandler, onError:errorHandler});
_loaderMax.append( new XMLLoader("yourFile.xml", {name:"xmlDoc"}) );
_loaderMax.load();
}
 
private function completeHandler(event:LoaderEvent):void {
_xml = LoaderMax.getContent("xmlDoc");
}
 
private function errorHandler(event:LoaderEvent):void {
	trace("error occured with " + event.target + ": " + event.text);
}

Avoid Twitter API-IP Call Limit

Twitter limits the amount of calls you can make to its API and from one IP to a few hundred an hour. So that means if you have something on the page that is showing your latest tweets, it will only work for the first few hundred people. We have a really high traffic site and needed a more robust solution.

What we did is create a php file that loads the XML from the twitter user timeline (example) and saves it on our server. We setup a chron job to call it every minute. The Flash movie loads the XML file that our php file creates for us ensuring that we will never go over the limit but always have the most up to date content from Twitter displayed on our website.

If you have any questions or want an example of the source, post in the comments. Running Example.

Resources Used:

  • The LoadXML and LoadURL files from here.
  • The AxisScroller from here.
  • Tweenlite
  • AS3 – Load XML Document Class Example

    One thing that I do quite often in Flash is load XML. In order to save time, I separated a lot of that code out into external class files. I also created a snippet of code that calls those files in order to save time when I want to use them. (You will need Lee Brimlow’s snippets panel for this to work.)

    The LoadXML and LoadURL classes are probably very similar to those found at Learning Actionscript 3 as that is where I learned a lot about this topic previously. Hope you find this helpful!

      Load XML files (774.5 KiB, 471 hits)

    Code snippet (for the panel) after the break…

    Read more

     

    Switch to our mobile site