使用得是Emeditor ,在看XML文档时,总是因为格式混乱而看不清。这个是一个Emeditor宏来自动格式化XML。下载:formatXml.rar (1,021.00 bytes)下面这段是这个网页版的javascript格式化XML的代码。 复制代码 代码如下: Xml格式化工具 <BR> String.prototype.removeLineEnd = function()<BR> {<BR> return this.replace(/(<.+?\s+?)(?:\n\s*?(.+?=".*?"))/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*?,">\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> //alert([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' + prefix + all;<BR> return ret;<BR> });<br><br> var prefixSpace = -1;<BR> var outputText = output.substring(1);<BR> //alert(outputText);<br><br> //把注释还原并解码,调格式<BR> outputText = outputText.replace(/\n/g,'\r').replace(/(\s*)<!--(.+?)-->/g,function($0, prefix, text)<BR> {<BR> //alert(['[',prefix,']=',prefix.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' + prefix + '<!--' + text.replace(/^\s*/mg, prefix ) + '-->';<BR> //alert(ret);<BR> return ret;<BR> });<br><br> return outputText.replace(/\s+$/g,'').replace(/\r/g,'\r\n');<BR> }<BR> function getPrefix(prefixIndex)<BR> {<BR> var span = ' ';<BR> var output = [];<BR> for(var i = 0 ; i < prefixIndex; ++i)<BR> {<BR> output.push(span);<BR> }<br><br> return output.join('');<BR> } <BR> function btnFormat_click()<BR> {<BR> var $ = document.getElementById;<BR> $('output').value = formatXml($('input').value);<BR> }<br><br>