XML自動縮排輸出

來源:互聯網
上載者:User
¢ ★XML縮排輸出,最簡單的方法。
By: 無心(cuixiping)  2005-1-28
時不時的有人問xml的縮排。
網上見的多的是孟子E章BLOG上的XSLT方法(他的blog宣傳不錯,名氣大嘛)
    網址: http://blog.csdn.net/net_lover/archive/2004/07/27/53601.aspx
    文章: 利用xslt對xml進行縮排格式化處理
該方法可以解決問題,但是,不僅代碼繁瑣,而且還需要一個xsl檔案。
我覺得是過於麻煩了。
其實一切可以更簡單的、更方便的。
問題來源於 MSXML DOM 調用save方法後,有改動的節點將都在一行,
沒有斷行符號換行和縮排,閱讀很不方便。
你想,微軟不會不知道這個問題存在吧,
微軟畢竟是微軟,他總不會沒有替你想個解決辦法吧。
都沒仔細看微軟的文檔,就來瞎折騰,把可以簡單的搞複雜了。
建議大家如果用微軟的 msxml ,就看看他的文檔吧。
我現在貼一下My Code:
★ASP中:
<%
Dim rdr, wrt
set rdr = Server.CreateObject("MSXML2.SAXXMLReader.4.0")
set wrt = Server.CreateObject("MSXML2.MXXMLWriter.4.0")
wrt.indent = True
Set rdr.contentHandler = wrt
'--------------------------------------------
' 可以解析 xml檔案、xmlDom對象、xml字串
' rdr.parseURL ("books.xml");
' rdr.parse (xmlDoc);
' rdr.parse ("<article><author>無心(cuixiping)</author></article>");
'--------------------------------------------
rdr.parse "<article><author>無心(cuixiping)</author></article>"
response.write ("<xmp>")
response.write (wrt.output)
response.write ("</xmp>")
%>
★JavaScript中:
<script language="JavaScript">
var rdr = new ActiveXObject("MSXML2.SAXXMLReader.4.0");
var wrt = new ActiveXObject("MSXML2.MXXMLWriter.4.0");
wrt.indent = true;
rdr.contentHandler = wrt;
/*
 * 可以解析 xml檔案、xmlDom對象、xml字串
 * rdr.parseURL ("books.xml");
 * rdr.parse (xmlDoc);
 * rdr.parse ("<article><author>無心(cuixiping)</author></article>");
 */
rdr.parse("<article><author>無心(cuixiping)</author></article>");
alert(wrt.output);
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.