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)!

相关专题

更多
Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

公务员递补名单公布时间 公务员递补要求
公务员递补名单公布时间 公务员递补要求

公务员递补名单公布时间不固定,通常在面试前,由招录单位(如国家知识产权局、海关等)发布,依据是原入围考生放弃资格,会按笔试成绩从高到低递补,递补考生需按公告要求限时确认并提交材料,及时参加面试/体检等后续环节。要求核心是按招录单位公告及时响应、提交材料(确认书、资格复审材料)并准时参加面试。

44

2026.01.15

公务员调剂条件 2026调剂公告时间
公务员调剂条件 2026调剂公告时间

(一)符合拟调剂职位所要求的资格条件。 (二)公共科目笔试成绩同时达到拟调剂职位和原报考职位的合格分数线,且考试类别相同。 拟调剂职位设置了专业科目笔试条件的,专业科目笔试成绩还须同时达到合格分数线,且考试类别相同。 (三)未进入原报考职位面试人员名单。

58

2026.01.15

国考成绩查询入口 国考分数公布时间2026
国考成绩查询入口 国考分数公布时间2026

笔试成绩查询入口已开通,考生可登录国家公务员局中央机关及其直属机构2026年度考试录用公务员专题网站http://bm.scs.gov.cn/pp/gkweb/core/web/ui/business/examResult/written_result.html,查询笔试成绩和合格分数线,点击“笔试成绩查询”按钮,凭借身份证及准考证进行查询。

11

2026.01.15

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

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

65

2026.01.14

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

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

36

2026.01.13

PHP 高性能
PHP 高性能

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

75

2026.01.13

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

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

21

2026.01.13

PHP 文件上传
PHP 文件上传

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

35

2026.01.13

热门下载

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

精品课程

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

共18课时 | 4.6万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 2万人学习

CSS教程
CSS教程

共754课时 | 19.1万人学习

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

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