加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用正则表达式格式化xml代码

发布时间:2020-12-14 01:33:00 所属栏目:百科 来源:网络整理
导读:!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"htmlhead meta http-equiv=content-type content="text/html; charset=UTF-8" titleXml格式化工具/title script type="text/javascript"String.prototype.removeLineEnd = function(){return t
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv=content-type content="text/html; charset=UTF-8">
    <title>Xml格式化工具</title>
    <script type="text/javascript">
		String.prototype.removeLineEnd = function(){
			return this.replace(/(<.+?s+?)(?:ns*?(.+?=".*?"))/g,'$1 $2')
		}
		function formatXml(text)
		{
			/*
			 * 功能:去掉多余的空格
			 * 
			 * $0为正则表达式/(<w+)(s.*?>)/g匹配到的字符串
			 * name为正则表达式(<w+)匹配到的字符串
			 * props为正则表达式(s.*?>)匹配到的字符串
			 */
			text = 'n' + text.replace(/(<w+)(s.*?>)/g,function($0,name,props){
				//alert('$0='+$0+',name='+name+',props='+props);
				//alert(name + ' ' + props.replace(/s+(w+)/g," $1"));
				return name + ' ' + props.replace(/s+(w+)/g," $1");
			}).replace(/>s*?</g,">n<");
			
			//把注释编码
			text = text.replace(/n/g,'r').replace(/<!--(.+?)-->/g,text)
			{
				var ret = '<!--' + escape(text) + '-->';
				//alert(ret);
				return ret;
			}).replace(/r/g,'n');
			
			//调整格式
			var rgx = /n(<(([^?]).+?)(?:s|s*?>|s*?(/)>)(?:.*?(?:(?:(/)>)|(?:<(/)2>)))?)/mg;
			var nodeStack = [];
			var output = text.replace(rgx,all,isBegin,isCloseFull1,isCloseFull2,isFull1,isFull2){
				var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');
				//alert([all,isClosed].join('='));
				var prefix = '';
				if(isBegin == '!')
				{
					prefix = getPrefix(nodeStack.length);
				}
				else 
				{
					if(isBegin != '/')
					{
						prefix = getPrefix(nodeStack.length);
						if(!isClosed)
						{
							nodeStack.push(name);
						}
					}
					else
					{
						nodeStack.pop();
						prefix = getPrefix(nodeStack.length);
					}

				
				}
					var ret =  'n' + prefix + all;
					return ret;
			});
        
			var prefixSpace = -1;
			var outputText = output.substring(1);
			//alert(outputText);
			
			//把注释还原并解码,调格式
			outputText = outputText.replace(/n/g,'r').replace(/(s*)<!--(.+?)-->/g,prefix,text)
			{
				//alert(['[',']=',prefix.length].join(''));
				if(prefix.charAt(0) == 'r')
					prefix = prefix.substring(1);
				text = unescape(text).replace(/r/g,'n');
				var ret = 'n' + prefix + '<!--' + text.replace(/^s*/mg,prefix ) + '-->';
				//alert(ret);
				return ret;
			});
			
			return outputText.replace(/s+$/g,'').replace(/r/g,'rn');

		}

    function getPrefix(prefixIndex)
    {
        var span = '    ';
        var output = [];
        for(var i = 0 ; i < prefixIndex; ++i)
        {
            output.push(span);
        }
        
        return output.join('');
    }        
        function btnFormat_click()
        {
            var $ = document.getElementById;
            $('output').value = formatXml($('input').value);
        }
        
    </script>
    
    
</head>

<body>
    <textarea id="input" style="width:100%;height:45%;">
		<a id="test" name="nn"		><b>b</b><c>c</c></a>
	</textarea>
    <div style="margin:0px auto;"><input type="button" id="btnFormat" onclick="btnFormat_click()" value="格式化" style="width:50%;height:10%;"/></div>
    <textarea id="output" style="width:100%;height:45%;"></textarea>

</body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读