10Jan

[AS 3] Parsing an XML file with XML and XMLList Classes

1 comment so far

Full documentation: XML Class, XMLList Class

Here is my imagedisplay.xml file contents:

<?xml version=”1.0″ encoding=”iso-8859-1″?>
<website>
<page>
<image>shark</image>
<client>Charlesbridge Publishing</client>
<caption>lorem ipsum</caption>
</page>
<page>
<image>elephant</image>
<client>Weybridge Publishing</client>
<caption>cool xml stuff</caption>
</page>
</website>

.. and here is my Actionscript file to parse the above xml file:

package
{
	import flash.display.Sprite;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.xml.XMLDocument;

	public class LoadXML extends Sprite
	{
		private var xml:XML;
		private var captionList:XMLList;

		public function LoadXML()
		{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, xmlDisplay);
			loader.load(new URLRequest("xmldisplay.xml"));
		}

		private function xmlDisplay(e:Event):void
		{
			xml = new XML(e.target.data);

			trace ("XML File");
			trace ("===================");
			trace (xml);

			// Capture the Page Element
			trace ("\n\nCaptures the Page Element");
			trace ("===================");
			trace (xml.page);

			// Captures Caption Element in the Page Element
			trace ("\n\nCaptures Caption Element in the Page Element");
			trace ("===================");
			trace (xml.page.caption);

			// Capture just the text of the Caption Element
			trace ("\n\nCaptures Caption Element's Text in the Page Element");
			trace ("===================");
			trace (xml.page.caption.text());

			// Capture text of of the Second Caption Element
			trace ("\n\nCaptures Caption Element's Text in the Second Page Element");
			trace ("===================");
			trace (xml.page.caption.text()[1]);

			// Capture The Caption Element's in the XML file
			trace ("\n\nCaption Elements");
			trace ("===================");

			var captionList:XMLList = xml.page.caption;

			for each (var captionElement:XML in captionList)
			{
				trace (captionElement);
			}
		}
	}
}

Share/Save/Bookmark

Thursday, January 10th, 2008 at 10:57 am and is filed under Actionscript 3, Actionscript XML. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “[AS 3] Parsing an XML file with XML and XMLList Classes”

  1. Posted by Vivek 3rd June, 2008 at 2:44 am

    would you please parse some xml file with some attributes?

Leave a reply