使用xmldom在服务器端生成静态html页面

来源:互联网 发布:襄阳程序员工资 编辑:程序博客网 时间:2024/06/07 19:36
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
服务器端的生成的页面数据,为了提高访问速度,往往需要生成静态的htm页面

通常,可以使用fso生成静态的htm页。

但如果是fso被禁止或没有使用fso的权限,就需要其他的方法来解决.

利用xmldom,使用其save()方法就是一个很好的解决之道.

而且,如果数据是xml格式,使用save()比使用fso速度要快,代码的复用率也高。

但需要注意的是:

调用xmldom.save()方法时,默认的编码方式是"uft-8".

如果指定文档输出类型为"html",由于其不可指定编码类型,当数据中含有中文字符,就会发现保存的htm数据中,所有中文字符都变成了乱码.

解决方法:

机制,通常浏览器对于html类型的页面,对于htm标记以外的标记并不进行解释.

a.指定输出文档类型为"xml"

b.指定编码(encoding="gb2312")

c.指定保留缩进格式(以方便阅读)

examples:

/*** create.asp ***/
<%
dim cXMLFile, cXSLFile
dim oXML, XSL
dim oOutput
dim chtmlFile, cOutputFile
chtmlFile = "book.htm"
'chtmlFile = "book_" & replace( replace( replace (now,":",""), "-", ""), " ", "") & ".htm
"cXMLFile = Server.MapPath("book.xml")
cXSLFile = Server.MapPath("book.xsl")
cOutputFile = Server.MapPath(chtmlFile)Set oXML = Server.CreateObject("Microsoft.xmldom")
oXML.async = false
oXML.load(cXMLFile)
Set oXSL = Server.CreateObject("Microsoft.xmldom")
oXSL.async = false
oXSL.load(cXSLFile)
Set oOutput = Server.CreateObject("Microsoft.xmldom")
Call oXML.transformNodeToObject(oXSL, oOutput)
oOutput.save (cOutputFile)
Set oXML = Nothing
Set oXSL = Nothing
Set oOutput = Nothing
Response.redirect(chtmlFile)
%>

/*** book.xml ***/
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<moonpiazza>
<book>
    <书名>基于XML 的 ASP.NET开发</书名>
  <定价>42</定价>
    <作者>Dan Wahlin/王宝良</作者>
</book>
<book>
    <书名>XML应用的UML建模技术</书名>
    <定价>32</定价>
    <作者>David Carlson/周靖 侯奕萌 沈金河等</作者>
</book>
<book>
    <书名>极限编程研究</书名>
    <定价>70</定价>
    <作者>Giancarrio Succi/Michele Marchesi/张辉(译)</作者>
</book>
<book>
    <书名>Design Patterns</书名>
    <定价>38</定价>
    <作者>Erich Gamma/Richard Helm/Ralph Johnson/John Vlissides</作者>
</book>
</moonpiazza>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击