AS3从XML中删除名称空间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3从XML中删除名称空间相关的知识,希望对你有一定的参考价值。
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.
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
以上是关于AS3从XML中删除名称空间的主要内容,如果未能解决你的问题,请参考以下文章