0

0

给XML文档添加新 ”records”

黄舟

黄舟

发布时间:2017-02-11 15:54:38

|

1735人浏览过

|

来源于php中文网

原创

本文所举的例子与保存html格式数据至xml类似。在以往当表格被提交后,我们通常会创建一个新的文档,现在只要文档已经存在,那么直接添加就可以了。此种技术的使用与创建基本数据类似。

  在前面的文章里,我已经演示了如何使用xmldom。因此,我们可以直接进入本文的示例。

  我们需要考虑的第一件事是我们将用于添加新"记录"的html 表单。在"将html表单数据保存至xml"例子中我们已使用过此表单,只是更改了文件名,但代码是相同的。

  addcontact.html: 

 
   
    Contact Information  
   
   
   
   

Enter your contact information

   First Name:    
Last Name:    
Address #1:    
Address #2:    
Phone Number:    
E-Mail:    
   
   
     


  我们设置此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,这样我们可以决定是否需要加上 一条处理指令。如果文件存在,我们就不需要加上这条指令。但是,如果创建了一个新的文件,那么则一定得加上这条处理指令。


企业后台管理系统JaManage2.0 多语言版
企业后台管理系统JaManage2.0 多语言版

功能介绍: 一.系统管理:管理员管理,可以新增管理员及修改管理员密码;数据库备份,为保证您的数据安全本系统采用了数据库备份功能;上传文件管理,管理你增加产品时上传的图片及其他文件 二.企业信息:可设置修改企业的各类信息及介绍 三.产品管理:产品类别新增修改管理,产品添加修改以及产品的审核 四.下载中心:可分类增加各种文件,如驱动和技术文档等文件的下载 五.订单管理:查看订单的详细信息

下载
'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)!

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.5万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 2万人学习

CSS教程
CSS教程

共754课时 | 19万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号