使用得是Emeditor ,在看XML文档时,总是因为格式混乱而看不清。这个是一个Emeditor宏来自动格式化XML。下载:formatXml.rar (1,021.00 bytes)下面这段是这个网页版的javascript格式化XML的代码。 复制代码 代码如下: Xml 格式化工具 String.prototype.removeLineEnd = function()<BR> {<BR> return this.replace(/(<. ?s ?)(?:ns*?(. ?=".*?" ))/g,'$1 $2')<BR> }<BR> function formatXml(text)<BR> {<BR> // 去掉空白的空格<BR> text = 'n' text.replace(/(< ;w )(s.*?>)/g,function($0, name, props)<BR> {<BR> return name ' ' props.replace(/s (w =)/g," $1") ;<BR> }).replace(/>s*?</g,">n<");<br><br> // 将编码注释<BR> text = text.replace(/n /g,'r').replace(/<!--(. ?)-->/g,function($0, text)<BR> {<BR> var ret = '<!--' escape(text) '-->';<BR> //alert(ret);<BR> return ret;<BR> }).replace(/r/g,'n');<br><br> // 调整格式<BR> var rgx = /n(<(([^?]). ?)(?:s|s*?>|s*?(/)>)(?:.*?(?:(?:(/)>)|(?:<(/)2> ;)))?)/mg;<BR> var nodeStack = [];<BR> var output = text.replace(rgx,function($0,all,name,isBegin,isCloseFull1,isCloseFull2 ,isFull1,isFull2){<BR> var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');<BR> //警报([all,isClosed].join('='));<BR> var prefix = '';<BR> if(isBegin == '!')<BR> {<BR> prefix = getPrefix(nodeStack.length) );<BR> }<BR> else <BR> {<BR> if(isBegin != '/')<BR> {<BR> prefix = getPrefix(nodeStack.length);<BR> if(!isClosed) <BR> {<BR> nodeStack.push(name);<BR> }<BR> }<BR> else<BR> {<BR> nodeStack.pop();<BR> prefix = getPrefix(nodeStack.length) ;<BR> }<br><br> }<BR> var ret = 'n' 前缀全部;<BR> return ret;<BR> });<br><br> var prefixSpace = -1;<BR> var outputText = 输出。子串(1);<BR> //alert(outputText);<br><br> // 将注释还原并解码,调整格式<BR> outputText = outputText.replace(/n/g,'r').replace(/(s* )<!--(. ?)-->/g,function($0, prefix, text)<BR> {<BR> //alert(['[',前缀,']=',前缀. length].join(''));<BR> if(prefix.charAt(0) == 'r')<BR> prefix = prefix.substring(1);<BR> text = unescape(text).replace (/r/g,'n');<BR> var ret = 'n' 前缀 '<!--' text.replace(/^s*/mg, prefix ) '-->';<BR> //alert(ret);<BR> return ret;<BR> });<br><br> return outputText.replace(/s $/g,'').replace(/r/g,'rn ');<BR> }<BR> function getPrefix(prefixIndex)<BR> {<BR> var span = ' ';<BR> var output = [];<BR> for(var i = 0 ; i <BR> prefixIndex; i)<BR> {<BR> output.push(span);<br> }<br><BR> return output.join('');<BR> } <BR> function bt nFormat_click()<BR> {<BR> var $ = document.getElementById;<BR> $('output').value = formatXml($('input').value);<br> }<br><br> ; < ;/textarea>