1:XML缺少數(shù)據(jù)庫具備的特性:高效的存儲(即使是數(shù)據(jù)庫內(nèi)XML字段還是沒有傳統(tǒng)字段的速度快)、索引和數(shù)據(jù)修改機(jī)制(DB2等數(shù)據(jù)庫產(chǎn)品對XML內(nèi)的元素和屬性可以加索引,內(nèi)建在數(shù)據(jù)庫中的XML字段享受了數(shù)據(jù)庫帶來的一些好處);嚴(yán)格的數(shù)據(jù)安全訪問控制;完整的事務(wù)和數(shù)據(jù)一致性控制;多用戶訪問機(jī)制;觸發(fā)器、完善的并發(fā)控制等因此,用戶量大、數(shù)據(jù)集成度高以及性能要求高的數(shù)據(jù)環(huán)境中還是需要數(shù)據(jù)庫來完成任務(wù),IBM的DB2工程師也在討論時(shí)建議我們統(tǒng)計(jì)類數(shù)據(jù)還是用傳統(tǒng)字段為好。
如果數(shù)據(jù)要被傳送到非 Windows 平臺上的應(yīng)用程序,那么把數(shù)據(jù)保存在 XML 文件中是有好處的。請記住,XML 有很強(qiáng)的跨平臺可移植性,并且數(shù)據(jù)無需轉(zhuǎn)換!
首先,我們將學(xué)習(xí)如何創(chuàng)建并保存一個(gè) XML 文件。下面的這個(gè) XML 文件將被命名為 "test.xml",并被保存在服務(wù)器上的 c 目錄中。我們將使用 ASP 和微軟的 XMLDOM 對象來創(chuàng)建并保存這個(gè) XML 文件:
<%Dim xmlDoc, rootEl, child1, child2, p'創(chuàng)建XML文檔Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")'創(chuàng)建根元素并將之加入文檔Set rootEl = xmlDoc.createElement("root")xmlDoc.appendChild rootEl'創(chuàng)建并加入子元素Set child1 = xmlDoc.createElement("child1")Set child2 = xmlDoc.createElement("child2")rootEl.appendChild child1rootEl.appendChild child2'創(chuàng)建 XML processing instruction'并把它加到根元素之前Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'")xmlDoc.insertBefore p,xmlDoc.childNodes(0)'把文件保存到 C 目錄xmlDoc.Save "c:\test.xml"%>
如果您打開這個(gè)被保存的文件,它會(huì)使這個(gè)樣子 ("test.xml"):
<?xml version="1.0"?><root> <child1 /> <child2 /></root>
現(xiàn)在,我們看一個(gè)真實(shí)的表單例子。
我們首先看一下這個(gè)被用在例子中的 HTML 表單:下面的HTML表單要求用戶輸入他們的名字、國籍以及電子郵件地址。隨后這些信息會(huì)被寫到一個(gè) XML 文件,以便存儲。
<html><body><form action="saveForm.asp" method="post"><h1>請輸入您的聯(lián)系信息:</h1><label>名字: </label><p><input type="text" id="firstName" name="firstName"></p><label>姓氏: </label><p><input type="text" id="lastName" name="lastName"></p><label>國家: </label><p><input type="text" id="country" name="country"></p><label>郵件: </label><p><input type="text" id="email" name="email"></p><p><input type="submit" id="btn_sub" name="btn_sub" value="Submit"><input type="reset" id="btn_res" name="btn_res" value="Reset"></p></form></body></html>
用于以上 HTML 表單的 action 被設(shè)置為 "saveForm.asp"。"saveForm.asp" 文件是一個(gè) ASP 頁面,可循環(huán)遍表單域,并把它們的值存儲在一個(gè) XML 文件中:
<%dim xmlDocdim rootEl,fieldName,fieldValue,attIDdim p,i'如果有錯(cuò)誤發(fā)生,不允許程序終止On Error Resume NextSet xmlDoc = server.CreateObject("Microsoft.XMLDOM")xmlDoc.preserveWhiteSpace=true'創(chuàng)建并向文檔添加根元素Set rootEl = xmlDoc.createElement("customer")xmlDoc.appendChild rootEl'循環(huán)遍歷 Form 集for i = 1 To Request.Form.Count '除去表單中的 button 元素 if instr(1,Request.Form.Key(i),"btn_")=0 then '創(chuàng)建 field 和 value 元素,以及 id 屬性 Set fieldName = xmlDoc.createElement("field") Set fieldValue = xmlDoc.createElement("value") Set attID = xmlDoc.createAttribute("id") '把當(dāng)前表單域的名稱設(shè)置為 id 屬性的值 attID.Text = Request.Form.Key(i) '把 id 屬性添加到 field 元素 fieldName.setAttributeNode attID '把當(dāng)前表單域的值設(shè)置為 value 元素的值 fieldValue.Text = Request.Form(i) '將 field 元素作為根元素的子元素進(jìn)行添加 rootEl.appendChild fieldName '將 value 元素作為 field 元素的子元素進(jìn)行添加 fieldName.appendChild fieldValue end ifnext'添加 XML processing instruction'并把它加到根元素之前Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'")xmlDoc.insertBefore p,xmlDoc.childNodes(0)'保存 XML 文件xmlDoc.save "c:\Customer.xml"'釋放所有的對象引用set xmlDoc=nothingset rootEl=nothingset fieldName=nothingset fieldValue=nothingset attID=nothingset p=nothing'測試是否有錯(cuò)誤發(fā)生if err.number<>0 then response.write("Error: No information saved.")else response.write("Your information has been saved.")end if%>
注釋:如果指定的 XML 文件名已經(jīng)存在,那個(gè)文件會(huì)被覆蓋!
XML 文件會(huì)由上面的代碼生成,大致的樣子是這樣的:("Customer.xml"):
<?xml version="1.0" ?><customer> <field id="firstName"> <value>David</value> </field> <field id="lastName"> <value>Smith</value> </field> <field id="country"> <value>China</value> </field> <field id="email"> <value>mymail@myaddress.com</value> </field></customer
vb中使用DomDocument對象操作xml時(shí),若要使用XPath中的內(nèi)部函數(shù),需要進(jìn)行如下設(shè)置
dim dom as DomDocument
dom.setProperty "SelectionLanguage", "XPath"
XML 路徑語言 (XPath) 查詢可以用來利用 DOM 方法 (如 selectNodes 或 selectSingleNode 查詢 XML 文件。預(yù)設(shè)會(huì)使用的查詢是 XSLPattern 回溯相容性。若要用以 XPath 變更 [以 XPath 的 [DOMDocument 的 SelectionLanguage 內(nèi)部屬性]。XPath 新增很多的功能 ; 比方說它可讓您使用如 字串長度 和 加總 函數(shù)。
下列的程式碼範(fàn)例示範(fàn)如何使用 XPath 與 selectNodes 方法:
Dim dom As DOMDocument30Dim nodelist As IXMLDOMNodeListDim strPath As String Set dom = New DOMDocument30dom.async = False dom.loadXML "<Admin><Area AreaName='a'/></Admin>" dom.setProperty "SelectionLanguage", "XPath"strPath = "/Admin/Area[string-length(@AreaName) = 1]"Set nodelist = dom.documentElement.selectNodes(strPath) Debug.Print "Found " & nodelist.length & " Node"
4,執(zhí)行該應(yīng)用程式,並請注意 [即時(shí)運(yùn)算] 視窗會(huì)顯示 找到 1 節(jié)點(diǎn)。
5,若要顯示預(yù)設(shè)的註解出呼叫 setProperty 程式碼行行為。因?yàn)樵賵?zhí)行程式碼會(huì)產(chǎn)生錯(cuò)誤訊息 XSL 模式比對不支援 字串長度 函式。