How to use JavaScript regular expressions to format XML content _javascript tips

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.