flex解析xml两方式

 


www.diy567.com累了,去上面休息一下下,QQ空间,美文,非主流,网络日记,搞笑短信,祝福短信,热门短信,有意思啊

1.使用httpService

2.使用urlLoader

Flex_mxml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://flex2.group.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%0A%3Cmx%3AApplication%20xmlns%3Amx%3D%22http%3A%2F%2Fwww.adobe.com%2F2006%2Fmxml%22%20layout%3D%22absolute%22%20creationComplete%3D%22init()%3Bloaderxml()%3B%22%20verticalScrollPolicy%3D%22off%22%20horizontalScrollPolicy%3D%22off%22%3E%0A%09%3Cmx%3AHTTPService%20id%3D%22parseXml%22%20showBusyCursor%3D%22true%22%20url%3D%22brow.xml%22%3E%0A%09%09%0A%09%3C%2Fmx%3AHTTPService%3E%0A%09%3Cmx%3AScript%3E%0A%09%09%3C!%5BCDATA%5B%0A%09%09%09import%20mx.collections.ArrayCollection%3B%0A%09%09%09import%20mx.rpc.events.FaultEvent%3B%0A%09%09%09import%20mx.rpc.events.ResultEvent%3B%0A%09%09%09private%20var%20arr%3AArrayCollection%0A%09%09%09private%20function%20init()%7B%0A%09%09%09%09parseXml.send()%3B%0A%09%09%09%09parseXml.addEventListener(ResultEvent.RESULT%2Cresult)%3B%0A%09%09%09%09parseXml.addEventListener(FaultEvent.FAULT%2Cfault)%3B%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09private%20function%20fault(event%3AFaultEvent)%7B%0A%09%09%09%09trace(event.message)%0A%09%09%09%7D%0A%09%09%09private%20function%20result(event%3AResultEvent)%7B%0A%09%09%09%09trace(%22result%3A%22%2Bevent.result.brows.brow%20as%20ArrayCollection)%3B%2F%2F%E8%BE%93%E5%87%BAnull%3B%E5%8D%95%E4%B8%8B%E8%BE%B9%0A%09%09%09%09arr%3Devent.result.brows.brow%20%2F%2F%E8%BF%99%E5%8F%AF%E4%BB%A5%E8%BD%AC%E6%88%90ArrayCollecation%0A%09%09%09%09trace(arr.getItemAt(0).sp.toString())%3B%2F%2F%E8%BE%93%E5%87%BAkang%0A%09%09%09%09trace(%22label%3A%22%2Barr.getItemAt(0).label.toString())%3B%2F%2F%E8%BE%93%E5%87%BA%22%22%0A%09%09%09%09%0A%09%09%09%09trace(parseXml.lastResult.brows.brow%5B0%5D.sp)%3B%2F%2F%E8%BE%93%E5%87%BAkang%0A%09%09%09%09trace(parseXml.lastResult.brows.brow%5B1%5D.icon)%2F%2F%E8%BE%93%E5%87%BA%20img%2FFace%2F1.swf%0A%09%09%09%7D%0A%09%09%09private%20function%20loaderxml()%7B%0A%09%09%09%09var%20url%3D%22brow.xml%22%3B%0A%09%09%09%09var%20ul%3AURLLoader%3Dnew%20URLLoader()%3B%0A%09%09%09%09ul.load(new%20URLRequest(url))%3B%0A%09%09%09%09ul.addEventListener(Event.COMPLETE%2CareadyLoad)%3B%09%0A%09%09%09%7D%0A%09%09%09private%20function%20areadyLoad(event%3AEvent)%7B%0A%09%09%09%09var%20xml%3AXML%3DXML(event.target.data)%3B%0A%09%09%09%09trace(xml.brow%5B0%5D.sp)%3B%2F%2F%E8%BE%93%E5%87%BAkang%0A%09%09%09%09trace(xml.brow%5B1%5D.%40data)%3B%2F%2F%E8%BE%93%E5%87%BA1%0A%09%09%09%09%0A%09%09%09%09var%20obj1%3AObject%3D%22%3Csp%3Ehello%3C%2Fsp%3E%22%3B%0A%09%09%09%09var%20obj2%3AObject%3D%22%3Css%3Esdfasf%3C%2Fss%3E%22%0A%09%09%09%09xml.appendChild(obj1)%3B%0A%09%09%09%09trace(xml.toXMLString())%3B%2F%2F%E6%9C%80%E5%90%8E%E5%8A%A0%E4%BA%86%E4%B8%80%E5%8F%A5%3Csp%3Ehello%3C%2Fsp%3E%0A%09%09%09%7D%0A%09%09%5D%5D%3E%0A%09%3C%2Fmx%3AScript%3E%0A%3C%2Fmx%3AApplication%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" layout= "absolute" creationComplete= "init();loaderxml();" verticalScrollPolicy= "off" horizontalScrollPolicy= "off" >
  3. <mx:HTTPServiceid="parseXml" showBusyCursor= "true" url= "brow.xml" >
  4. </mx:HTTPService>
  5. <mx:Script>
  6. <![CDATA[
  7. importmx.collections.ArrayCollection;
  8. importmx.rpc.events.FaultEvent;
  9. importmx.rpc.events.ResultEvent;
  10. privatevararr:ArrayCollection
  11. privatefunctioninit(){
  12. parseXml.send();
  13. parseXml.addEventListener(ResultEvent.RESULT,result);
  14. parseXml.addEventListener(FaultEvent.FAULT,fault);
  15. }
  16. privatefunctionfault(event:FaultEvent){
  17. trace(event.message)
  18. }
  19. privatefunctionresult(event:ResultEvent){
  20. trace("result:" +event.result.brows.browasArrayCollection);//输出null;单下边
  21. arr=event.result.brows.brow//这可以转成ArrayCollecation
  22. trace(arr.getItemAt(0 ).sp.toString());//输出kang
  23. trace("label:" +arr.getItemAt( 0 ).label.toString());//输出 ""
  24. trace(parseXml.lastResult.brows.brow[0 ].sp);//输出kang
  25. trace(parseXml.lastResult.brows.brow[1 ].icon)//输出img/Face/ 1 .swf
  26. }
  27. privatefunctionloaderxml(){
  28. varurl="brow.xml" ;
  29. varul:URLLoader=newURLLoader();
  30. ul.load(newURLRequest(url));
  31. ul.addEventListener(Event.COMPLETE,areadyLoad);
  32. }
  33. privatefunctionareadyLoad(event:Event){
  34. varxml:XML=XML(event.target.data);
  35. trace(xml.brow[0 ].sp);//输出kang
  36. trace(xml.brow[1 ].@data);//输出 1
  37. varobj1:Object="<sp>hello</sp>" ;
  38. varobj2:Object="<ss>sdfasf</ss>"
  39. xml.appendChild(obj1);
  40. trace(xml.toXMLString());//最后加了一句<sp>hello</sp>
  41. }
  42. ]]>
  43. </mx:Script>
  44. </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();loaderxml();" verticalScrollPolicy="off" horizontalScrollPolicy="off">
	<mx:HTTPService id="parseXml" showBusyCursor="true" url="brow.xml">
		
	</mx:HTTPService>
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			private var arr:ArrayCollection
			private function init(){
				parseXml.send();
				parseXml.addEventListener(ResultEvent.RESULT,result);
				parseXml.addEventListener(FaultEvent.FAULT,fault);
			}
			
			private function fault(event:FaultEvent){
				trace(event.message)
			}
			private function result(event:ResultEvent){
				trace("result:"+event.result.brows.brow as ArrayCollection);//输出null;单下边
				arr=event.result.brows.brow //这可以转成ArrayCollecation
				trace(arr.getItemAt(0).sp.toString());//输出kang
				trace("label:"+arr.getItemAt(0).label.toString());//输出""
				
				trace(parseXml.lastResult.brows.brow[0].sp);//输出kang
				trace(parseXml.lastResult.brows.brow[1].icon)//输出 img/Face/1.swf
			}
			private function loaderxml(){
				var url="brow.xml";
				var ul:URLLoader=new URLLoader();
				ul.load(new URLRequest(url));
				ul.addEventListener(Event.COMPLETE,areadyLoad);	
			}
			private function areadyLoad(event:Event){
				var xml:XML=XML(event.target.data);
				trace(xml.brow[0].sp);//输出kang
				trace(xml.brow[1].@data);//输出1
				
				var obj1:Object="<sp>hello</sp>";
				var obj2:Object="<ss>sdfasf</ss>"
				xml.appendChild(obj1);
				trace(xml.toXMLString());//最后加了一句<sp>hello</sp>
			}
		]]>
	</mx:Script>
</mx:Application>

brow.xml

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://flex2.group.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbrows%3E%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%3Cbrow%20data%3D%220%22%20tp%3D%22%E5%BE%AE%E7%AC%91%22%20label%3D%22%22%20icon%3D%22img%2FFace%2F0.swf%22%3E%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%3Csp%3Ekang%3C%2Fsp%3E%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fbrow%3E%0A%09%09%09%09%3Cbrow%20data%3D%221%22%20tp%3D%22%E6%92%87%E5%98%B4%22%20label%3D%22a%22%20icon%3D%22img%2FFace%2F1.swf%22%2F%3E%0A%09%09%09%09%3Cbrow%20data%3D%222%22%20tp%3D%22%E6%92%87%E5%98%B4%22%20label%3D%22b%22%20icon%3D%22img%2FFace%2F2.swf%22%2F%3E%0A%09%09%09%09%3Cbrow%20data%3D%223%22%20tp%3D%22%E6%92%87%E5%98%B4%22%20label%3D%22c%22%20icon%3D%22img%2FFace%2F3.swf%22%2F%3E%0A%09%09%09%09%3Cbrow%20data%3D%224%22%20tp%3D%22%E6%92%87%E5%98%B4%22%20label%3D%22d%22%20icon%3D%22img%2FFace%2F4.swf%22%2F%3E%0A%09%09%09%09%3Cbrow%20data%3D%225%22%20tp%3D%22%E6%92%87%E5%98%B4%22%20label%3D%22e%22%20icon%3D%22img%2FFace%2F5.swf%22%2F%3E%0A%3Cbrows%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < brows >
  3. < brow data = "0" tp = "微笑" label = "" icon = "img/Face/0.swf" >
  4. < sp > kang </ sp >
  5. </ brow >
  6. < brow data = "1" tp = "撇嘴" label = "a" icon = "img/Face/1.swf" />
  7. < brow data = "2" tp = "撇嘴" label = "b" icon = "img/Face/2.swf" />
  8. < brow data = "3" tp = "撇嘴" label = "c" icon = "img/Face/3.swf" />
  9. < brow data = "4" tp = "撇嘴" label = "d" icon = "img/Face/4.swf" />
  10. < brow data = "5" tp = "撇嘴" label = "e" icon = "img/Face/5.swf" />
  11. < brows >
本文转自:http://flex2.group.iteye.com/group/blog/381820

你可能感兴趣的:(xml,qq,Flex,Flash,Adobe)