這篇文章主要介紹了如何XML文檔添加新 ”records”,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
洛江ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話(huà)聯(lián)系或者加微信:18982081108(備注:SSL證書(shū)合作)期待與您的合作!
如何XML文檔添加新 ”records”
AddContact.html:
<html> <head> <title> Contact Information </title> </head> <body> <form action="processAdd.asp" method="post"> <h4>Enter your contact information</h4> 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>
我們?cè)O(shè)置此HTML表單是來(lái)處理ADD。ASP的。這里的ASP 頁(yè)面具有檢測(cè)XML.文件及ROLODEX.XML是否存在的功能。如果它們確實(shí)存在,ASP則會(huì)在文件上附加新的條目,如果文件不存在,則需要?jiǎng)?chuàng)建。
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 %>
如果你已經(jīng)讀過(guò)關(guān)于"將HTML 表單數(shù)據(jù)保存至XML格式"的文章,你會(huì)注意到附加到將HTML數(shù)據(jù)擴(kuò)展到XML文件的代碼與HTML數(shù)據(jù)擴(kuò)展到新文檔的代碼基本上是一致的。但是這里還是有兩個(gè)主要的不同點(diǎn):
'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
本節(jié)的代碼來(lái)自addNewContacttoXML 功能。因?yàn)槲覀儾豢赡苊看味夹陆ㄒ粋€(gè)文件,所以我們改為保存CONTACT。如果能夠LOAD此文件呢,我們則獲得了這個(gè)XML文檔的根元素;如果不能夠呢,那么我們就假設(shè)它不存在并創(chuàng)建一個(gè)新的要元素并將它附加到XML文檔上。
另外一個(gè)主要區(qū)別在于:當(dāng)我們對(duì)文件進(jìn)行二次檢測(cè),是否成功的LOAD,這樣我們可以決定是否需要加上 一條處理指令。如果文件存在,我們就不需要加上這條指令。但是,如果創(chuàng)建了一個(gè)新的文件,那么則一定得加上這條處理指令。
'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
除開(kāi)以上兩點(diǎn)不同之處外,你可以發(fā)現(xiàn) 保存數(shù)據(jù)至新文件的代碼實(shí)際上是與 附加新record至存在文件的代碼是一樣的。我們創(chuàng)建一個(gè)新的element, contact CONTAINER,以便能容下每個(gè)新添的RECORD。代碼將會(huì)在Form Collection of the Request Objec中不斷重復(fù)以創(chuàng)建適合的XML節(jié)點(diǎn)并將這些節(jié)點(diǎn)值設(shè)置得與當(dāng)前Form Field.一樣。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何XML文檔添加新 ”records””這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
標(biāo)題名稱(chēng):如何XML文檔添加新”records”
瀏覽地址:http://www.rwnh.cn/article2/jgpjic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站改版、虛擬主機(jī)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)