熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

如何使用Javascript正則表達式來格式化XML內容

2022-06-13   來源: JSP教程 

  使用得是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>


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20047.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.