/****** ".","[]","..","@" 四个常用的E4X操作符 ******/
//使用"."操作符查询节点,得到第一个子节点,使用"[]"访问指定索引的节点
//使用".."操作符访问所有以该标签命名的节点,而忽略节点的上下关系
//使用"@"操作符访问节点属性
代码段readXml,changeXml有详细用法
百度 http://www.baidu.com assets/baidu.gif 新浪 http://www.sina.com assets/sina.gif 开心网 http://www.kaixin001.com assets/kaixin001.gif QQ邮箱 http://mail.qq.com assets/qqmail.gif 谷歌 http://www.google.com.cn assets/google.gif
" + myText + ""; var testXmlObj:XML = new XML(str); trace(testXmlObj.toXMLString()); } //------------------------------------------------ ----------- // HttpService部分 //------------------------------------------------ ----------- private function httpServiceGetXml():void { var httpservice:HTTPService = new HTTPService(); //httpservice.url = "http://localhost:2286/myxml.xml"; httpservice.url = "http://localhost:2286/mybook.xml"; httpservice.useProxy = false; //httpservice.resultFormat = "array"; //结果要转换为ArrayCollection应用到List,DataGrid时使用 httpservice.resultFormat = "e4x"; //Tree控件 httpservice.addEventListener(ResultEvent.RESULT,on HttpServiceResultHandler); httpservice.send(); } private function onHttpServiceResultHandler(event:ResultEvent):void { var strresult:String = event.result.toString(); var externalXML:XML = new XML(strresult); trace(externalXML.toXMLString()); //转换XML为ArrayCollection显示到数据容器中部分, 此时httpService的resultFormat需要为"array" //list = event.result[0].items.item; //转移为XMLListCollection,应用到Tree xmlList = event.result.category; } //------------------------------------------------ ----------- // URLLoader部分 //------------------------------------------------ ----------- private function urlLoaderGetXml():void { var urlLoader:URLLoader = new URLLoader(); var urlRequest:URLRequest = new URLRequest("myxml.xml"); urlLoader.load(urlRequest); urlLoader.addEventListener(Event.COMPLETE,onUrlLoa derHandler); } private function onUrlLoaderHandler(event:Event):void { var urlLoader2:URLLoader = event.target as URLLoader; if(urlLoader2 != null) { var externalXML:XML = new XML(urlLoader2.data); trace(externalXML.toXMLString()); //readXml(externalXML); //changeXml(externalXML); } else { trace("urlLoader2 is not a URLLoader!"); } } //------------------------------------------------ ----------- // 操作XML部分 //------------------------------------------------ ----------- private function readXml(xml:XML):void { if(xml == null) return; /****** ".","[]","..","@" 四个常用的E4X操作符 ******/ //使用"."操作符查询节点,得到第一个子节点,使用"[]"访问指定索引的节点 writeMsg(xml.item[0]); //使用".."操作符访问所有以该标签命名的节点,而忽略节点的上下关系 writeMsg(xml..name); //使用"@"操作符访问节点属性 writeMsg(xml.item[0].@name); writeMsg(xml.item[0].name.@url); //过滤,只显示name为"百度"的节点。属性和节点都可过滤 writeMsg(xml.item.(name=="百度")); //修改 writeMsg(xml.item[0].(name="百度一下就知道")); } private function changeXml(xml:XML):void { if(xml == null) return; /****** 修改操作 XML对象包括修改,添加和删除操作******/ //使用insertChildBefore和insertChildAfter方法在某一位置添加元素/节点,appendChild和prependChild方法在最前和最后添加元素/节点 xml.insertChildAfter(xml.item[0],); xml.insertChildBefore(xml.item[0],); xml.appendChild(); xml.prependChild(); //writeMsg(xml); //添加、修改属性 xml.item[0].@date="20100721"; //writeMsg(xml); //添加、修改XML对象 xml.item[0].name = "prependChild"; xml.item[0].url = "http://www.prependChild.com"; writeMsg(xml); writeMsg("============"); //删除节点、属性等 delete xml.item[0].@date; delete xml.item[0].name; delete xml.item[0].url.text()[0]; writeMsg(xml); } private function writeMsg(s:String):void { txtarea.text += s + "\n"; } ]]-->
//使用"."操作符查询节点,得到第一个子节点,使用"[]"访问指定索引的节点
//使用".."操作符访问所有以该标签命名的节点,而忽略节点的上下关系
//使用"@"操作符访问节点属性
代码段readXml,changeXml有详细用法
百度 http://www.baidu.com assets/baidu.gif 新浪 http://www.sina.com assets/sina.gif 开心网 http://www.kaixin001.com assets/kaixin001.gif QQ邮箱 http://mail.qq.com assets/qqmail.gif 谷歌 http://www.google.com.cn assets/google.gif
" + myText + ""; var testXmlObj:XML = new XML(str); trace(testXmlObj.toXMLString()); } //------------------------------------------------ ----------- // HttpService部分 //------------------------------------------------ ----------- private function httpServiceGetXml():void { var httpservice:HTTPService = new HTTPService(); //httpservice.url = "http://localhost:2286/myxml.xml"; httpservice.url = "http://localhost:2286/mybook.xml"; httpservice.useProxy = false; //httpservice.resultFormat = "array"; //结果要转换为ArrayCollection应用到List,DataGrid时使用 httpservice.resultFormat = "e4x"; //Tree控件 httpservice.addEventListener(ResultEvent.RESULT,on HttpServiceResultHandler); httpservice.send(); } private function onHttpServiceResultHandler(event:ResultEvent):void { var strresult:String = event.result.toString(); var externalXML:XML = new XML(strresult); trace(externalXML.toXMLString()); //转换XML为ArrayCollection显示到数据容器中部分, 此时httpService的resultFormat需要为"array" //list = event.result[0].items.item; //转移为XMLListCollection,应用到Tree xmlList = event.result.category; } //------------------------------------------------ ----------- // URLLoader部分 //------------------------------------------------ ----------- private function urlLoaderGetXml():void { var urlLoader:URLLoader = new URLLoader(); var urlRequest:URLRequest = new URLRequest("myxml.xml"); urlLoader.load(urlRequest); urlLoader.addEventListener(Event.COMPLETE,onUrlLoa derHandler); } private function onUrlLoaderHandler(event:Event):void { var urlLoader2:URLLoader = event.target as URLLoader; if(urlLoader2 != null) { var externalXML:XML = new XML(urlLoader2.data); trace(externalXML.toXMLString()); //readXml(externalXML); //changeXml(externalXML); } else { trace("urlLoader2 is not a URLLoader!"); } } //------------------------------------------------ ----------- // 操作XML部分 //------------------------------------------------ ----------- private function readXml(xml:XML):void { if(xml == null) return; /****** ".","[]","..","@" 四个常用的E4X操作符 ******/ //使用"."操作符查询节点,得到第一个子节点,使用"[]"访问指定索引的节点 writeMsg(xml.item[0]); //使用".."操作符访问所有以该标签命名的节点,而忽略节点的上下关系 writeMsg(xml..name); //使用"@"操作符访问节点属性 writeMsg(xml.item[0].@name); writeMsg(xml.item[0].name.@url); //过滤,只显示name为"百度"的节点。属性和节点都可过滤 writeMsg(xml.item.(name=="百度")); //修改 writeMsg(xml.item[0].(name="百度一下就知道")); } private function changeXml(xml:XML):void { if(xml == null) return; /****** 修改操作 XML对象包括修改,添加和删除操作******/ //使用insertChildBefore和insertChildAfter方法在某一位置添加元素/节点,appendChild和prependChild方法在最前和最后添加元素/节点 xml.insertChildAfter(xml.item[0],); xml.insertChildBefore(xml.item[0],); xml.appendChild(); xml.prependChild(); //writeMsg(xml); //添加、修改属性 xml.item[0].@date="20100721"; //writeMsg(xml); //添加、修改XML对象 xml.item[0].name = "prependChild"; xml.item[0].url = "http://www.prependChild.com"; writeMsg(xml); writeMsg("============"); //删除节点、属性等 delete xml.item[0].@date; delete xml.item[0].name; delete xml.item[0].url.text()[0]; writeMsg(xml); } private function writeMsg(s:String):void { txtarea.text += s + "\n"; } ]]-->