使用得是Emeditor 在看XML文档时总是因为格式混乱而看不清这个是一个Emeditor宏来自动格式化XML下载formatXmlrar ( bytes)
下面这段是这个网页版的javascript格式化XML的代码
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
<html>
<head>
<meta httpequiv=contenttype content="text/html; charset=UTF">
<title>Xml格式化工具</title>
<script type="text/javascript">
StringprototyperemoveLineEnd = function()
{
return thisreplace(/(<+?s+?)(?:ns*?(+?="*?"))/g$ $)
}
function formatXml(text)
{
//去掉多余的空格
text = n + textreplace(/(<w+)(s*?>)/gfunction($ name props)
{
return name + + propsreplace(/s+(w+=)/g" $");
})replace(/>s*?</g">n<");
//把注释编码
text = textreplace(/n/gr)replace(/<!(+?)>/gfunction($ text)
{
var ret = <! + escape(text) + >;
//alert(ret);
return ret;
})replace(/r/gn);
//调整格式
var rgx = /n(<(([^?])+?)(?:s|s*?>|s*?(/)>)(?:*?(?:(?:(/)>)|(?:<(/)>)))?)/mg;
var nodeStack = [];
var output = textreplace(rgxfunction($allnameisBeginisCloseFullisCloseFull isFullisFull){
var isClosed = (isCloseFull == /) || (isCloseFull == / ) || (isFull == /) || (isFull == /);
//alert([allisClosed]join(=));
var prefix = ;
if(isBegin == !)
{
prefix = getPrefix(nodeStacklength);
}
else
{
if(isBegin != /)
{
prefix = getPrefix(nodeStacklength);
if(!isClosed)
{
nodeStackpush(name);
}
}
else
{
nodeStackpop();
prefix = getPrefix(nodeStacklength);
}
}
var ret = n + prefix + all;
return ret;
});
var prefixSpace = ;
var outputText = outputsubstring();
//alert(outputText);
//把注释还原并解码调格式
outputText = outputTextreplace(/n/gr)replace(/(s*)<!(+?)>/gfunction($ prefix text)
{
//alert([[prefix]=prefixlength]join());
if(prefixcharAt() == r)
prefix = prefixsubstring();
text = unescape(text)replace(/r/gn);
var ret = n + prefix + <! + textreplace(/^s*/mg prefix ) + >;
//alert(ret);
return ret;
});
return outputTextreplace(/s+$/g)replace(/r/grn);
}
function getPrefix(prefixIndex)
{
var span = ;
var output = [];
for(var i = ; i < prefixIndex; ++i)
{
outputpush(span);
}
return outputjoin();
}
function btnFormat_click()
{
var $ = documentgetElementById;
$(output)value = formatXml($(input)value);
}
</script>
</head>
<body>
<textarea id="input" style="width:%;height:%;"></textarea>
<div style="margin:px auto;"><input type="button" id="btnFormat" onclick="btnFormat_click()" value="格式化" style="width:%;height:%;"/></div>
<textarea id="output" style="width:%;height:%;"></textarea>
</body>
</html>