/ Published in: ActionScript 3
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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
URL: http://brianmriley.wordpress.com/2008/03/14/remove-xml-namespaces-in-flex-or-as3/