Using EmEditor, when looking at an XML document, it's always confusing because of the format confusion. This is a EmEditor macro to automatically format the XML. Download:
Formatxml.rar(1,021.00 bytes)
The following section is the JavaScript formatted XML code for this page version.
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<meta http-equiv=content-type content= "text/html; Charset=utf-8 ">
<title>xml Formatting Tools </title>
<script type= "Text/javascript" >
String.prototype.removeLineEnd = function ()
{
Return This.replace (/(<.+?\s+?) (?:\ N\s*? (.+?=".*?")) /g, ' $ $ '
}
function Formatxml (text)
{
Get rid of extra space
Text = ' \ n ' + text.replace (/(<\w+) (\s.*?>)/g,function ($, name, props)
{
Return name + ' + props.replace (/\s+ (\w+=)/g, "$");
). Replace (/>\s*?</g, ">\n<");
Encode the annotation
Text = Text.replace (/\n/g, ' \ R '). Replace (/<!--(. +?) -->/g,function ($, text)
{
var ret = ' <!--' + escape (text) + '--> ';
alert (ret);
return ret;
). Replace (/\r/g, ' \ n ');
Adjust formatting
var rgx =/\n (< ([^\?]). +?) (?:\ S|\s*?>|\s*? (\/) >) (?:. *? (?:(:( \/) >) | (?:< (\/) \2>)) /MG;
var nodestack = [];
var output = Text.replace (Rgx,function ($0,all,name,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);
To restore and decode the annotation, and to adjust the format
Outputtext = Outputtext.replace (/\n/g, ' \ R '). Replace (/(\s*) <!--(. +?) -->/g,function ($, prefix, text)
{
alert ([' [', prefix, ']= ', 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, ' \ r \ n ');
}
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>
<body>
<textarea id= "Input" style= "width:100%;height:45%;" ></textarea>
<div style= "margin:0px auto;" ><input type= "button" id= "Btnformat" onclick= "Btnformat_click ()" value= "format" style= "width:50%;height:10%;" /></div>
<textarea id= "Output" style= "width:100%;height:45%;" ></textarea>
</body>