概述: 本文檔介紹了如何借助XML語言實(shí)現(xiàn)在Web頁面上的OWC對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行展示的方法。由于XML數(shù)據(jù)可以跨越防火墻,因此該方式可以實(shí)現(xiàn)在Internet上對數(shù)據(jù)進(jìn)行展現(xiàn)。
在基于WEB的數(shù)據(jù)庫分析應(yīng)用中,常常借助OWC控件結(jié)合HTML實(shí)現(xiàn)對數(shù)據(jù)的表格和圖表兩種方式的展現(xiàn)。一般應(yīng)用往往采用OWC直接連接數(shù)據(jù)庫的方式,這會使數(shù)據(jù)庫連接口令暴露在客戶端,而使數(shù)據(jù)庫的安全性降低。本文介紹的采用XML作為OWC和數(shù)據(jù)庫之間數(shù)據(jù)交換介質(zhì)的方式,能夠避免這種對數(shù)據(jù)庫造成的不安全危險。同時,這也能帶來其他一些好處,例如:使瀏覽器與WEB服務(wù)器及數(shù)據(jù)庫間的交互次數(shù)減少,從而加快排序等操作的響應(yīng)速度,并減少了服務(wù)器的負(fù)荷。
一、獲取XML數(shù)據(jù)
將數(shù)據(jù)庫中的數(shù)據(jù)轉(zhuǎn)化為XML格式的方法很多,本文檔不過多對此進(jìn)行介紹。從性能、通用性角度考慮,這里我們采用了ADO直接序列化(持久化Persist)數(shù)據(jù)的方式,代碼如下:
<!--GetData.asp -->
<%
dim strConn
strConn="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=test;Data Source=jlwz"
’----------讀取數(shù)據(jù)----------------
dim conn,rs
set conn=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
conn.Open strConn
rs.Open "Select Stat_Date,Call_Num,Call_Fee From CallStat",conn
’將ADO轉(zhuǎn)化為xml dom
Const adPersistXML=1
dim objXMLDom
set objXMLDom=server.CreateObject("MSXML2.DOMDocument.3.0")
rs.Save objXMLDom,adPersistXML
set rs=nothing
%>
這種方式得到的的XML并不夠簡潔,其中包含了Schema信息。
盡管對于OWC中的DataSourceControl控件來說,可以直接采用這種形式的XML數(shù)據(jù),但考慮到數(shù)據(jù)從服務(wù)器向客戶端傳輸?shù)男剩覀兪褂肵SLT對這種XML數(shù)據(jù)進(jìn)行了轉(zhuǎn)化。為此,編寫了如下的Clean.xsl文件:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:element name="data">
<xsl:for-each select="/xml/rs:data/z:row">
<xsl:element name="row">
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
然后,在GetData.asp中通過如下代碼對前面的XML數(shù)據(jù)進(jìn)行轉(zhuǎn)化:
’用XSLT清理轉(zhuǎn)化XML數(shù)據(jù)
Dim strCleanXML,objXSLT
set objXSLT=server.CreateObject("MSXML2.DOMDocument")
objXSLT.load(server.MapPath("Clean.xsl"))
strCleanXML=objXMLDom.transformNode(objXSLT)
此時,就得到了我們想要的比較簡潔的XML結(jié)構(gòu)的字符串,可以簡單地將其Response.Write到客戶端:
<data>
<row>
<Stat_Date>2003-06-01</Stat_Date>
<Call_Num>100</Call_Num>
<Call_Fee>200</Call_Fee>
</row>
<row>
<Stat_Date>2003-07-01</Stat_Date>
<Call_Num>200</Call_Num>
<Call_Fee>400</Call_Fee>
</row>
。。。
</data>
二、使用OWC控件和HTML表格展現(xiàn)XML數(shù)據(jù)
2.1 基本功能的實(shí)現(xiàn)
這里新建了另外一個HTML頁面。為了使用剛才得到的XML數(shù)據(jù),在HTML頁面中,采用XML 數(shù)據(jù)島:
<XML id="dbXML" src="getData.asp" onreadystatechange="init()"></XML>
然后,可以利用HTML表格的綁定功能展現(xiàn)數(shù)據(jù):
<table datasrc="#dbXML" style="width:100%;BORDER-COLLAPSE: collapse;" border=1 cellpadding=0 cellspacing=0>
<tr>
<td><div type=text datafld=Stat_Date></div></td>
<td><div type=text datafld=Call_Num></div></td>
<td><div type=text datafld=Call_Fee></div></td>
</tr>
</table>
在剛才的XML數(shù)據(jù)島的onreadystatechange事件對應(yīng)的init()函數(shù)中,我們通過如下代碼實(shí)現(xiàn)OWC的圖表:
<OBJECT id=CS1 style="WIDTH:400px;TOP:0px;HEIGHT:280px"
classid=clsid:0002E556-0000-0000-C000-000000000046 VIEWASTEXT>
</OBJECT>
<script lanaguage=vbscript>
Sub init()
if(dbXML.readyState="complete") then
dim strXML
set strXML=dbXML.XMLDocument
createChart strXML,CS1
end if
End Sub
Sub createChart(byref oxml,cspace) ’根據(jù)傳入的XML生成圖表
Dim xdoc,xroot,cCnt
Dim ndx,cnode,txtData,txtCat,txtData2
Set xdoc=dbXML.XMLDocument
Set xroot = xdoc.documentElement
cCnt = xroot.childNodes.length
txtData = "":txtCat = ""
’ 從XML數(shù)據(jù)中得到相應(yīng)的子符串
For ndx = 0 To cCnt - 1
Set cnode = xroot.childNodes(ndx)
txtCat = txtCat & cnode.childNodes(0).text
txtData = txtData & cnode.childNodes(1).text
txtData2=txtData2 & cnode.childNOdes(2).text
if ndx <> (cCnt -1) then
txtCat = txtCat & ","
txtData = txtData & ","
txtData2 = txtData2 & ","
end if
Next
’---下面開始繪圖----------
’添加數(shù)據(jù)序列1
set ch =cspace.Charts.Add()
set s = ch.SeriesCollection.Add()
s.name="通話費(fèi)用(元)"
s.Caption=s.name
s.SetData c.chDimCategories,c.chDataLiteral, txtCat
s.SetData c.chDimValues, c.chDataLiteral, txtData
s.type=8 ’曲線圖
’設(shè)定時間刻度軸格式
Set axCategory = cspace.Charts(0).Axes(c.chAxisPositionCategory)
with axCategory
.GroupingUnitType = c.chAxisUnitMonth ’月
.GroupingUnit = 1 ’單位
.NumberFormat="Short Date" ’短日期
end with
’添加數(shù)據(jù)序列2
set s2 = ch.SeriesCollection.Add()
s2.name="通話次數(shù)(次)"
s2.Caption=s2.name
s2.SetData c.chDimValues, c.chDataLiteral, txtData2
’標(biāo)題
ch.HasTitle = true
ch.Title.Caption="通話情況月報"
ch.Title.font.color="black"
ch.Title.font.size=10
ch.Title.font.bold=true
’ChartSpace屬性
cspace.Border=c.chLineDash
cspace.HasSelectionMarks=true
cspace.AllowFiltering=true ’允許命令與分組
cspace.AllowPropertyToolbox=true
’設(shè)置圖例及位置
ch.Legend.Position=c.chLegendPositionRight
ch.HasLegend=false
’分成不同的組,顯示雙坐標(biāo)軸
s2.UnGroup TRUE
Set axIncomeAxis = ch.Axes.Add(s2.Scalings(c.chDimValues))
axIncomeAxis.Position = c.chAxisPositionRight
axIncomeAxis.HasMajorGridlines=false
s2.type=0 ’柱形圖
End Sub
這樣,我們就得到了數(shù)據(jù)表格和圖表,其最終效果如下:
這樣,借助于XML技術(shù)和IE綁定技術(shù),我們就實(shí)現(xiàn)了OWC對數(shù)據(jù)庫中數(shù)據(jù)的展示,而在客戶端并沒有暴露任何數(shù)據(jù)連接信息。
2.2 其他功能
OWC可以很容易的實(shí)現(xiàn)將所見到的圖表保存為本地圖片,大大方便了使用者。同時,OWC提供了多種圖表類型,如:餅圖、曲線圖、柱形圖等,適合在不同的情況下展現(xiàn)數(shù)據(jù)。
如果借助COM組件、以及對XSL的靈活運(yùn)用,我們這個頁面能得到更好的性能和更強(qiáng)的功能。比如:對HTML表格的排序(參見附件中的HTML源代碼)、數(shù)據(jù)分頁等。此外,我們還可以實(shí)現(xiàn)通用的數(shù)據(jù)訪問、搜索功能。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。