xml同类节点信息替换

前端之家收集整理的这篇文章主要介绍了xml同类节点信息替换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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>

猜你在找的XML相关文章