亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > XML > 正文

給XML文檔添加新 ”records”

2024-09-05 20:53:14
字體:
來源:轉載
供稿:網友
本文所舉的例子與保存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數據擴展到新文檔的代碼基本上是一致的。但是這里還是有兩個主要的不同點:

   '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.一樣。

  如以往一樣,我推薦大家復制以上代碼至你的 服務器上并運行。希望以上舉例會對你有所幫助。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲欧美三级伦理| 伊人一区二区三区久久精品| 久久精品视频一| 国产精品永久免费视频| 2019亚洲日韩新视频| 欧美在线免费观看| 亚洲曰本av电影| 在线成人免费网站| 91高清免费在线观看| 国产精品一区二区三区成人| 成人久久久久爱| 久久精品亚洲94久久精品| 亚洲欧美国内爽妇网| 欧美成人在线免费| 欧美成人午夜视频| 久久精品一区中文字幕| 欧美壮男野外gaytube| 中文字幕一区二区精品| 欧美精品www在线观看| 欧美日韩福利视频| 国产精品激情自拍| 一区二区三区www| 午夜精品在线观看| 黄色一区二区在线| 中文字幕日韩精品在线观看| 久久精品中文字幕一区| 亚洲一二三在线| 一本色道久久综合狠狠躁篇怎么玩| 欧美另类在线播放| zzijzzij亚洲日本成熟少妇| 日韩国产欧美精品在线| 亚洲美女www午夜| 久久久久久久久久久av| 自拍偷拍亚洲精品| 国产一区二区三区久久精品| 日韩视频欧美视频| 欧美影院成年免费版| 在线性视频日韩欧美| 国产精品免费一区豆花| 美女啪啪无遮挡免费久久网站| 在线看片第一页欧美| 久久久精品免费视频| 久久精品电影一区二区| 国产精品海角社区在线观看| 亚洲人成网站999久久久综合| 中文日韩在线视频| 久久国产精品首页| 日韩精品中文字幕视频在线| 97国产精品人人爽人人做| 色偷偷亚洲男人天堂| 国产精品一久久香蕉国产线看观看| 日韩成人网免费视频| 性色av香蕉一区二区| 亚洲最大av网站| 2018国产精品视频| 91精品国产色综合| 欧美日韩国产专区| 国产精品一区二区三区久久久| 91亚洲精品一区二区| 欧美电影在线免费观看网站| 欧美精品在线第一页| 欧美自拍大量在线观看| 久久久精品免费| 精品美女永久免费视频| 伊人伊成久久人综合网小说| 国产在线视频欧美| 欧美电影院免费观看| 日韩精品在线免费观看视频| 日韩av电影手机在线观看| 国产狼人综合免费视频| 欧美一级视频免费在线观看| 国产一区二区在线免费视频| 黄色一区二区三区| 精品夜色国产国偷在线| 国产成人在线视频| 欧美一级视频在线观看| 国产主播在线一区| 国产在线不卡精品| 亚洲精品国产拍免费91在线| 国产精品伦子伦免费视频| 亚洲四色影视在线观看| 日韩女在线观看| 国产91|九色| 久久成人这里只有精品| 亚洲乱码一区二区| 久久久久女教师免费一区| 国产精品h片在线播放| 欧美亚洲视频一区二区| 久久成人精品视频| 国产精品久久9| 菠萝蜜影院一区二区免费| 久久久999国产| 亚洲欧美成人精品| 日韩精品在线免费| 91在线观看免费高清完整版在线观看| 久久999免费视频| 欧美精品videosex极品1| 亚洲精品综合久久中文字幕| 亚洲国内高清视频| 亚洲午夜未删减在线观看| 欧美日韩国产页| 中文字幕亚洲综合| 欧美视频一区二区三区…| 亚洲一区二区三区在线视频| 久久久久久久国产精品| 国内成人精品视频| 久久综合久久八八| 日韩av免费在线观看| 欧美有码在线观看| 亚洲性生活视频在线观看| 亚洲第一偷拍网| 亚洲黄色在线看| 亚洲综合第一页| 成人激情视频网| 欧美丝袜一区二区| 成人久久一区二区三区| 久久夜色精品亚洲噜噜国产mv| 欧美大片在线看免费观看| 国产精品aaaa| 777午夜精品福利在线观看| 久久久久久久一| 在线日韩av观看| 精品久久久久久久久久国产| 少妇高潮久久久久久潘金莲| 亚洲自拍偷拍网址| 欧美日韩美女在线| 国产高清在线不卡| 国产精品精品一区二区三区午夜版| 国产精品小说在线| 亚洲欧洲日本专区| 中文字幕一区二区三区电影| 欧美日韩黄色大片| 黑人巨大精品欧美一区二区三区| 欧美日韩性视频| 亚洲欧美国产一本综合首页| 久99九色视频在线观看| 在线看片第一页欧美| 久久韩剧网电视剧| 青草成人免费视频| 亚洲aⅴ男人的天堂在线观看| 国产97人人超碰caoprom| 久久久免费在线观看| 国产日韩中文在线| 国产精自产拍久久久久久| 欧美日韩一区二区精品| 日韩一区二区av| 狠狠综合久久av一区二区小说| 国内久久久精品| 亚洲日韩中文字幕| 波霸ol色综合久久| 一区二区三区黄色| 欧美色xxxx| 国产精品电影在线观看| 精品福利在线看| 欧美大成色www永久网站婷| 国产综合久久久久| 国精产品一区一区三区有限在线| 91精品国产综合久久香蕉的用户体验| 色悠久久久久综合先锋影音下载| 欧美日韩aaaa| 中文国产亚洲喷潮| 久久精品一本久久99精品| 欧美另类极品videosbest最新版本| 日韩在线视频线视频免费网站|