Return to Snippet

Revision: 49322
at July 20, 2011 00:29 by adrianparr


Initial Code
package  {
	
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLLoaderDataFormat;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	public class Main extends MovieClip {
		
		public function Main() {
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			xmlLoader.addEventListener(Event.COMPLETE, onXmlLoader_COMPLETE);
			xmlLoader.addEventListener(IOErrorEvent.IO_ERROR , onXmlLoader_IO_ERROR);
			xmlLoader.load(new URLRequest("data.xml"));
		}
		
		private function onXmlLoader_COMPLETE(e:Event):void
		{
			var myXml:XML = new XML(e.target.data);
			var myXmlStr:String = myXml.toString();
			var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
			myXmlStr = myXmlStr.replace(xmlnsPattern, "");
			myXml = new XML(myXmlStr);
			trace(myXml.message);
		}
 
		private function onXmlLoader_IO_ERROR(event:IOErrorEvent):void
		{
			trace("onXmlLoader_IO_ERROR()");
			trace("event.text: "+String(event.text));
		}
		
	}
}

///////////////////////////////////////////////////////////////////
// The 'data.xml' looks like this ...
// 
// <?xml version="1.0" encoding="utf-8" ?>
// <root xmlns="http://www.adrianparr.com">
//     <message>Hello World</message>
// </root>
//
//
// And the output will be ...
//
// Hello World

Initial URL
http://brianmriley.wordpress.com/2008/03/14/remove-xml-namespaces-in-flex-or-as3/

Initial Description
If you don't deal with namespaces in XML correctly they can break your parsing in AS3. One way of getting around this is to remove the namespace from the XML a using regular expression.

Initial Title
AS3 Remove namespace from XML

Initial Tags
regex, xml, regexp

Initial Language
ActionScript 3