本文所舉的例子與保存HTML格式資料至XML類似。在以往當表格被提交後,我們通常會建立一個新的文檔,現在只要文檔已經存在,那麼直接新增就可以了。此種技術的使用與創建基本數據類似。
在前面的文章裡,我已經示範如何使用XMLDOM。因此,我們可以直接進入本文的範例。
我們需要考慮的第一件事是我們將用於新增"記錄"的HTML 表單。在"將HTML表單資料儲存至XML"範例中我們已使用過此表單,只是更改了檔案名,但程式碼是相同的。
AddContact.html:
<html> <head> <title> Contact Information </title> </head> <body> <form action="processAdd.asp" method="post"> <h3>Enter your contact information</h3> First Name: <input type="text" id="firstName" name="firstName"><br> Last Name: <input type="text" id="lastName" name="lastName"><br> Address #1: <input type="text" id="address1" name="address1"><br> Address #2: <input type="text" id="address2" name="address2"><br> Phone Number: <input type="text" id="phone" name="phone"><br> E-Mail: <input type="text" id="email" name="email"><br> <input type="submit" id="btnSub" name="btnSub" value="Submit"><br> </form> </body> </html>
我們設定此HTML表單是來處理ADD。 ASP的。這裡的ASP 頁面具有偵測XML.檔案及ROLODEX.XML是否存在的功能。如果它們確實存在,ASP則會在檔案上附加新的條目,如果檔案不存在,則需要建立。
Process Add.asp:
<% '-------------------------------------------------------------------- 'The "addNewContacttoXML" Function accepts two parameters. 'strXMLFilePath - The physical path where the XML file will be saved. 'strFileName - The name of the XML file that will be saved. '-------------------------------------------------------------------- Function addNewContacttoXML(strXMLFilePath, strFileName) 'Declare local variables. Dim objDom Dim objRoot Dim objRecord Dim objField Dim objFieldValue Dim objattID Dim objattTabOrder Dim objPI Dim blnFileExists Dim x 'Instantiate the Microsoft XMLDOM. Set objDom = server.CreateObject("Microsoft.XMLDOM") objDom.preserveWhiteSpace = True 'Call the Load Method of the XMLDOM Object. The Load ethod has a 'boolean return value indicating whether or not the file could be 'loaded. If the file exists and loads it will return true, otherwise, 'it will return false. blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName) 'Test to see if the file loaded successfully. If blnFileExists = True Then 'If the file loaded set the objRoot Object equal to the root element 'of the XML document. Set objRoot = objDom.documentElement Else 'Create your root element and append it to the XML document. Set objRoot = objDom.createElement("rolodex") objDom.appendChild objRoot End If 'Create the new container element for the new record. Set objRecord = objDom.createElement("contact") objRoot.appendChild objRecord 'Iterate through the Form Collection of the Request Object. For x = 1 To Request.Form.Count 'Check to see if "btn" is in the name of the form element. If it is, 'then it is a button and we do not want to add it to the XML 'document". If instr(1,Request.Form.Key(x),"btn") = 0 Then 'Create an element, "field". Set objField = objDom.createElement("field") 'Create an attribute, "id". Set objattID = objDom.createAttribute("id") 'Set the value of the id attribute equal the the name of the current 'form field. objattID.Text = Request.Form.Key(x) 'The setAttributeNode method will append the id attribute to the 'field element. objField.setAttributeNode objattID 'Create another attribute, "taborder". This just orders the 'elements. Set objattTabOrder = objDom.createAttribute("taborder") 'Set the value of the taborder attribute. objattTabOrder.Text = x 'Append the taborder attribute to the field element. 'objField.setAttributeNode objattTabOrder 'Create a new element, "field_value". Set objFieldValue = objDom.createElement("field_value") 'Set the value of the field_value element equal to the value of the 'current field in the Form Collection. objFieldValue.Text = Request.Form(x) 'Append the field element as a child of the new record container 'element, contact. objRecord.appendChild objField 'Append the field_value element as a child of the field element. objField.appendChild objFieldValue End If Next 'Check once again to see if the file loaded successfully. If it did 'not, that means we are creating a new document and need to be sure to 'insert the XML processing instruction. If blnFileExists = False then 'Create the xml processing instruction. Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'") 'Append the processing instruction to the XML document. objDom.insertBefore objPI, objDom.childNodes(0) End If 'Save the XML document. objDom.save strXMLFilePath & "\" & strFileName 'Release all of your object references. Set objDom = Nothing Set objRoot = Nothing Set objRecord = Nothing Set objField = Nothing Set objFieldValue = Nothing Set objattID = Nothing Set objattTabOrder = Nothing Set objPI = NothingEnd Function 'Do not break on an error. On Error Resume Next 'Call the addNewContacttoXML function, passing in the physical path to 'save the file to and the name that you wish to use for the file. addNewContacttoXML "c:","rolodex.xml" 'Test to see if an error occurred, if so, let the user know. 'Otherwise, tell the user that the operation was successful. If err.number <> 0 then Response.write("Errors occurred while saving your form submission.") Else Response.write("Your form submission has been saved.") End If %>
如果你已經讀過關於"將HTML 表單資料保存至XML格式"的文章,你會注意到附加到將HTML資料擴展到XML檔案的程式碼與HTML資料擴展到XML格式"的文章,你會注意到附加到將HTML資料擴展到XML檔案的程式碼與HTML資料擴展到XML格式"的文章,你會注意到附加到將HTML資料擴展到XML檔案的程式碼與HTML資料擴展到XML格式"新文檔的程式碼基本上是一致的。但這裡還是有兩個主要的不同點:
'Call the Load Method of the XMLDOM Object. The Load Method has a 'boolean return value indicating whether or not the file could be 'loaded. If the file exists and loads it will return true, otherwise, 'it will return false. blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName) 'Test to see if the file loaded successfully. If blnFileExists = True Then 'If the file loaded set the objRoot Object equal to the root element 'of the XML document. Set objRoot = objDom.documentElement Else 'Create your root element and append it to the XML document. Set objRoot = objDom.createElement("contact") objDom.appendChild objRoot End If
本節的程式碼來自addNewContacttoXML 功能。因為我們不可能每次都新建一個文件,所以我們改為保存CONTACT。如果能夠LOAD此文件呢,我們則獲得了這個XML文件的根元素;如果不能夠呢,那麼我們就假設它不存在並創建一個新的要元素並將它附加到XML文件上。
另一個主要區別在於:當我們對文件進行二次檢測,是否成功的LOAD,這樣我們可以決定是否需要加上 一條處理指令。如果檔案存在,我們就不需要加上這條指令。但是,如果建立了一個新的文件,那麼一定要加上這條處理指令。
'Check once again to see if the file loaded successfully. If it did 'not, that means we are creating a new document and need to be sure to 'insert the XML processing instruction. If blnFileExists = False then 'Create the xml processing instruction. Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'") 'Append the processing instruction to the XML document. objDom.insertBefore objPI, objDom.childNodes(0) End If
除開以上兩點不同之處外,你可以發現 保存資料至新檔案的程式碼實際上是與 附加新record至存在檔案的程式碼是一樣的。我們創建一個新的element, contact CONTAINER,以便能容下每個新添的RECORD。程式碼將會在Form Collection of the Request Objec中不斷重複以建立適合的XML節點並將這些節點值設定得與目前Form Field.相同。
以上就是為XML文件新增新 ”records」的內容,更多相關內容請關注PHP中文網(www.php.cn)!