xml中同属性值的替换,可以将xml转化为字符串。用字符串的操作根据正则表达式来修改。
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.utils.ObjectUtil; import mx.utils.UIDUtil; protected function init(event:FlexEvent):void { // testErgodicXml(); testReplaceXml(); } private var replaceXml:XML = <root> <node id="11"> <noneNode nodeId="11"> <node id="22"/> </noneNode> </node> <node id="33"/> </root>; private function testReplaceXml():void { replaceXMLproperty(replaceXml,"node","id"); } private function replaceXMLproperty(xml:XML,nodeName:String,pro:String):XML { var str:String = xml.toString(); for each (var num:XML in xml.descendants(nodeName)) { var myPattern:RegExp = new RegExp(num.@[pro],"g"); str = str.replace(myPattern,UIDUtil.createUID()); } xml = new XML(str); return xml; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <!--<fx:XML id="testXml" source="xmlEditor.xml"/>--> </fx:Declarations> </s:Application>