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, 25 hits)

Code snippet (for the panel) after the break…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//copy and paste this into the snippets.xml file
 
<snippet>
<title>LoadXMLDocumentClass</title>
<code><![CDATA[package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import fl.controls.UIScrollBar;
 
	import com.jeffguthrie.util.xml.LoadXML;
 
	public class LoadXMLDocumentClass extends Sprite {
 
		public var _sOutput_txt:TextField;
 
		private var _loadXML:LoadXML;
 
		public function LoadXMLDocumentClass():void {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
 
			_loadXML = new LoadXML("path to xml",true);
			_loadXML.addEventListener("loadxmlXMLLoaded", onLoadXML, false, 0, true);
			_loadXML.addEventListener("loadxmlErrorLoadingXML", onErrorLoadingXML, false, 0, true);
			_loadXML.addEventListener("loadxmlSecurityError", onSecurityError, false, 0);
			_loadXML.addEventListener("loadxmlIOError", onIOError, false, 0);
			_loadXML.addEventListener("loadxmlErrorParseXML", onErrorParseXML, false, 0);
		}
 
		private function onLoadXML(event:Event):void {
			var ws_xml:XML = new XML(_loadXML.xmlData);
			//trace('LoadXMLDocumentClass.onLoadXML: \n ' + ws_xml);
			_sOutput_txt.text = ws_xml.toXMLString();
 
			if (_sOutput_txt.textHeight > _sOutput_txt.height) {
				var scrollBar:UIScrollBar = new UIScrollBar();
				scrollBar.name = 'sb';
				scrollBar.scrollTarget = _sOutput_txt;  //assign the target of the scrollBar to your textfield
				scrollBar.height = _sOutput_txt.height;  //make the height the same as the textfield
				scrollBar.move(_sOutput_txt.x + _sOutput_txt.width, _sOutput_txt.y - 5);  //Move the scrollbar to the righthand side
				addChild(scrollBar);
				scrollBar.update();
			}
		}
		private function onErrorLoadingXML (e:Event):void {
			trace('LoadXMLDocumentClass.ErrorLoadingXML ' + e);
			//dispatchEvent(new Event("errorLoadingXML"));
		}
		private function onSecurityError (e:Event):void {
			trace('LoadXMLDocumentClass.onSecurityError ' + e)
		}
		private function onIOError (e:Event):void {
			trace('LoadXMLDocumentClass.onIOError ' + e);
		}
		private function onErrorParseXML (e:Event):void {
			trace('LoadXMLDocumentClass.onErrorParseXML ' + e);
		}
	}
}
]]></code>
</snippet>
Spread the Good Word:
  • Digg
  • del.icio.us
  • Facebook
  • NewsVine
  • StumbleUpon

, , , ,

  1. No comments yet.
(will not be published)