Flex及AS3的百多条小知识(3)
【新增属性】 var example:XML = <person/>; example.element = ""; example.element.@name = "Youthoy"; example.element.@["bad-variable-name"] = "yes"; example.element.@other = ["riahome.cn",undefined,true,44,null]; var id:int = 44; example.element.@["user" + id] = "love"; trace(example); /* 输出: <person> <element name="Youthoy" bad-variable-name="yes" other="riahome.cn," user44="love"/> </person> */ 【向XML插入文字元素和节点元素】 //在指定元素前和后插入,在最前端和尾端插入 var example:XML = <person/>; example.two = ""; example.insertChildBefore(example.two,<one/>); //在two节点前插入one节点 example.insertChildBefore(example.two,"在two节点前插入文字"); example.insertChildAfter(example.two,<three/>); //在two节点后插入three节点 example.prependChild(<start/>); //在最顶端插入start节点 example.appendChild(<end/>); //在尾端插入end节点 example.start = "start内容"; //向节点增加内容 trace(example); /* 输出: <person> <start>start内容</start> <one/> 在two节点前插入文字 <two/> <three/> <end/> </person> */ 【把XML实例的内容属性转换为任何可以转换成字符串的内容】 //先把等号右边转为可以转换的字符串 var example:XML = <person/>; example.one = "one"; example.two = new URLLoader(); example.three = new Sprite(); example.four = new Boolean; //Boolean对象的值初始化为false example.five = new Number(); example.six = new String(); example.seven = {a:"Youthoy",b:true,c:undefined}; example.eight = ["three",null]; /* 输出: <person> <one>one</one> <two>[object URLLoader]</two> <three>[object Sprite]</three> <four>false</four> <five>0</five> <six/> <seven>[object Object]</seven> <eight>three,</eight> </person> */ 【对XML属性层层递归的算法】 var example:XML = <menu> <menuitem label="File"> <menuitem label="New"/> <menuitem label="Save"/> </menuitem> <menuitem label="Help"> <menuitem label="About"/> </menuitem> </menu>; walk(example); function walk(node:XML):void { for each(var element:XML in node.elements()) { trace(element.@label); walk(element); } 【读取文字节点及其值】 var example:XML = <menu> My name is Youthoy. <myName>Youthoy</myName> </menu>; example.text(); //输出: My name is Youthoy. example.myName.toXMLString(); //输出: <myName>Youthoy</myName> //以下三行都输出: Youthoy example.myName.toString(); example.myName.text(); example.myName; 【递归元素属性时读取属性名与其值】 var example:XML = <person name="Youthoy" age="21"/>; for each(var i:XML in example.attributes()) { trace(i.name() + "=" + i); } /* 输出: name=Youthoy age=21 */ 另类方法: var example:XML = <person name="Youthoy" age="21"/>; trace(example.@*[1]); //输出: 21 【不论层级地访问整个XML中指定的节点或属性】 使用后代存取器运算符.. 例子1(对于节点元素): var example:XML = <a> <z>I am z.</z> <b> <c></c> <z>I am z.</z> </b> </a>; trace(example..z.text()); //输出: I am z.I am z. 例子2(对于属性): var example:XML = <person> <a> <item name="Youthoy" age="21"/> </a> <item name="Jimpa" age="21"/> </person> trace(example..@name); //输出: YouthoyJimpa 【删除节点元素,文字节点以及属性】 使用delete对单一项进行删除,要删除整个XMLList可以使用for循环以倒序形式进行删除,以避免迭代时需要改变数组索引问题. 【搜索XML的高级应用】 使用术语过滤.(condition),可结合正则表达式来使用. 例子: var example:XML = <foodgroup> <fruits> <fruit color="red">Apple</fruit> <fruit color="orange">Orange</fruit> <fruit color="green">Pear</fruit> <fruit color="red">Watermelon</fruit> </fruits> <vegetables> <vegetable color="red">Tomato</vegetable> <vegetable color="brown">Potato</vegetable> <vegetable color="green">Broccoli</vegetable> </vegetables> </foodgroup>; trace(example..*.(hasOwnProperty("@color") && @color == "red")); /* hasOwnProperty所做的检测是确保元素具有color属性,然后,有的话,就测试color属性之值是否为red.只有当条件的计算结果为true时,该元素才会加入EX4运算式所返回的XMLList. 输出 <fruit color="red">Apple</fruit> <fruit color="red">Watermelon</fruit> <vegetable color="red">Tomato</vegetable> */ 【CDATA(Character Data)标签】 <![CDATA[]]>,必须大写.如果<![CDATA[[]]]>这样的话,它会告诉你语法错误的. 【一些名词】 RPC: 全称: Remote Procedure Call,台湾的朋友称其为: 远端程序呼叫 SOAP: 全称: Simple Object Access Protocol,台湾的朋友称其为: 简单物体存取协定 WSDL: 全称: Web Service Description Language,台湾的朋友称其为: web service描述语言 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |