asp中建立XML檔案,自動縮排解決方案

來源:互聯網
上載者:User

在asp中使用dom建立新文檔時,新文檔不會進行自動縮排,雖從文檔內容方面來講新文檔是格式良好或有效,整個文檔均在一行,用記事本或其它編輯工作開啟新檔案時,瀏覽檔案很是不便,以下提供在asp中縮排XML的兩個解決辦法:
1.使用xsl進行縮排格式化:
   xsl檔案:

  <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "xml"  version="1.0" encoding="utf-8" omit-xml-declaration = "yes" indent = "yes" media-type="text/xml"/>

 <xsl:template match="/ | @* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
   asp:
   <%
dim xmldoc,xsldoc,resultdoc
dim node,subnode
set xmldoc=server.CreateObject("MSXML2.DOMDocument")
xmldoc.async=false
xmldoc.loadXML("<?xml version='1.0' encoding='utf-8'?><root/>") '裝載根節點

'----------------------建立節點*start----------------------
set node=xmldoc.createElement("user")
set subnode=xmldoc.createElement("name")
subnode.text="ssm1226"
node.appendChild subnode
set subnode=xmldoc.createElement("nickname")
subnode.text="雨中人"
node.appendChild subnode
set subnode=nothing
xmldoc.documentElement.appendChild node
set node=nothing
'----------------------建立節點*end----------------------
set xsldoc=server.CreateObject("MSXML2.DOMDocument")
xsldoc.async=false
xsldoc.load server.MapPath("transform.xsl")'裝載轉換xsl檔案

set resultdoc=server.CreateObject("MSXML2.DOMDocument")
resultdoc.async=false
xmldoc.transformNodeToObject xsldoc,resultdoc '轉換
set node=resultdoc.createProcessingInstruction("xml","version='1.0' encoding='utf-8'") '創加<?xml version='1.0' encoding='utf-8'?>聲明
resultdoc.insertBefore node,resultdoc.firstChild'添加聲明
set node=nothing
resultdoc.save server.MapPath("result.xml") '儲存XML檔案
%>

2.DOM中添加分行符號

<%
dim xmldoc,node1,node2
set xmldoc=server.CreateObject("msxml2.domdocument")
xmldoc.async=false
xmlDoc.validateOnParse = false
if not xmldoc.loadXML("<root></root>") then
 response.Write xmldoc.parseError.reason
 response.End
end if

response.Write xmldoc.xml
response.End
set node1=xmldoc.createTextNode(vbcrlf)

set node2=xmldoc.createElement("name")
node2.text="ssm1226"

xmldoc.documentElement.appendChild node1.cloneNode(true)
xmldoc.documentElement.appendChild node2
xmldoc.documentElement.appendChild node1.cloneNode(true)

xmldoc.save "c:/test.xml"

%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.