0

0

给XML文档添加新 ”records”

黄舟

黄舟

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

|

1735人浏览过

|

来源于php中文网

原创

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


企业后台管理系统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)!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
JavaScript浏览器渲染机制与前端性能优化实践
JavaScript浏览器渲染机制与前端性能优化实践

本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。

23

2026.03.06

Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

68

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

162

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

84

2026.03.04

Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

113

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

29

2026.03.03

Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

79

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

62

2026.02.28

Golang 性能分析与运行时机制:构建高性能程序
Golang 性能分析与运行时机制:构建高性能程序

Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。

51

2026.02.28

热门下载

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

精品课程

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

共18课时 | 6.7万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 3.2万人学习

CSS教程
CSS教程

共754课时 | 40.5万人学习

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

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