How to use JavaScript regular expressions to format XML content

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" >


<html>


<head>


<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+?) (?: ns*? (.+?=".*?")) /g, ' $ $ '


    }


function Formatxml (text)


    {


Remove the extra space


text = ' n ' + text.replace (/(<w+) (s.*?>)/g,function ($, name, props)


        {


return name + ' + props.replace (/s+ (w+=)/g, "$");


}). replace (/>s*?</g, ">n<");





//Coding the annotation


Text = Text.replace (/n/g, ' R '). Replace (/<!--(. +?) -->/g,function ($, text)


        {


var ret = ' <!--' + escape (text) + '--> ';


//alert (ret);


return ret;


}). replace (/r/g, ' n ');





//Adjustment format


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);





//The annotation is restored and decoded, and the format is adjusted


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, ' 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%;" ></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>


</html>

Related Article

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.