手机
当前位置:查字典教程网 >编程开发 >XML/RSS >给XML文档添加新 ”records”
给XML文档添加新 ”records”
摘要:本文所举的例子与保存HTML格式数据至XML类似。在以往当表格被提交后,我们通常会创建一个新的文档,现在只要文档已经存在,那么直接添加就可以...

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

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

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

AddContact.html:

复制代码 代码如下:

<html>

<head>

<title>ContactInformation</title>

</head>

<body>

<formaction="processAdd.asp"method="post">

<h3>Enteryourcontactinformation</h3>

FirstName:

<inputtype="text"id="firstName"name="firstName"><br>LastName:

<inputtype="text"id="lastName"name="lastName"><br>Address#1:

<inputtype="text"id="address1"name="address1"><br>Address#2:

<inputtype="text"id="address2"name="address2"><br>PhoneNumber:

<inputtype="text"id="phone"name="phone"><br>E-Mail:

<inputtype="text"id="email"name="email"><br>

<inputtype="submit"id="btnSub"name="btnSub"value="Submit"><br>

</form>

</body>

</html>

我们设置此HTML表单是来处理ADD。ASP的。这里的ASP页面具有检测XML.文件及ROLODEX.XML是否存在的功能。如果它们确实存在,ASP则会在文件上附加新的条目,如果文件不存在,则需要创建。

ProcessAdd.asp:

复制代码 代码如下:

<%

'--------------------------------------------------------------------

'The"addNewContacttoXML"Functionacceptstwoparameters.

'strXMLFilePath-ThephysicalpathwheretheXMLfilewillbesaved.

'strFileName-ThenameoftheXMLfilethatwillbesaved.

'--------------------------------------------------------------------

FunctionaddNewContacttoXML(strXMLFilePath,strFileName)

'Declarelocalvariables.

DimobjDom

DimobjRoot

DimobjRecord

DimobjField

DimobjFieldValue

DimobjattID

DimobjattTabOrder

DimobjPI

DimblnFileExists

Dimx

'InstantiatetheMicrosoftXMLDOM.

SetobjDom=server.CreateObject("Microsoft.XMLDOM")

objDom.preserveWhiteSpace=True

'CalltheLoadMethodoftheXMLDOMObject.TheLoadethodhasa

'booleanreturnvalueindicatingwhetherornotthefilecouldbe

'loaded.Ifthefileexistsandloadsitwillreturntrue,otherwise,

'itwillreturnfalse.

blnFileExists=objDom.Load(strXMLFilePath&""&strFileName)

'Testtoseeifthefileloadedsuccessfully.

IfblnFileExists=TrueThen

'IfthefileloadedsettheobjRootObjectequaltotherootelement

'oftheXMLdocument.

SetobjRoot=objDom.documentElementElse

'CreateyourrootelementandappendittotheXMLdocument.

SetobjRoot=objDom.createElement("rolodex")

objDom.appendChildobjRoot

EndIf

'Createthenewcontainerelementforthenewrecord.

SetobjRecord=objDom.createElement("contact")

objRoot.appendChildobjRecord

'IteratethroughtheFormCollectionoftheRequestObject.

Forx=1ToRequest.Form.Count

'Checktoseeif"btn"isinthenameoftheformelement.Ifitis,

'thenitisabuttonandwedonotwanttoaddittotheXML

'document".

Ifinstr(1,Request.Form.Key(x),"btn")=0Then

'Createanelement,"field".

SetobjField=objDom.createElement("field")

'Createanattribute,"id".

SetobjattID=objDom.createAttribute("id")

'Setthevalueoftheidattributeequalthethenameofthecurrent

'formfield.

objattID.Text=Request.Form.Key(x)

'ThesetAttributeNodemethodwillappendtheidattributetothe

'fieldelement.objField.setAttributeNodeobjattID

'Createanotherattribute,"taborder".Thisjustordersthe

'elements.

SetobjattTabOrder=objDom.createAttribute("taborder")

'Setthevalueofthetaborderattribute.

objattTabOrder.Text=x

'Appendthetaborderattributetothefieldelement.

'objField.setAttributeNodeobjattTabOrder

'Createanewelement,"field_value".

SetobjFieldValue=objDom.createElement("field_value")

'Setthevalueofthefield_valueelementequaltothevalueofthe

'currentfieldintheFormCollection.

objFieldValue.Text=Request.Form(x)

'Appendthefieldelementasachildofthenewrecordcontainer

'element,contact.objRecord.appendChildobjField

'Appendthefield_valueelementasachildofthefieldelement.

objField.appendChildobjFieldValue

EndIf

Next

'Checkonceagaintoseeifthefileloadedsuccessfully.Ifitdid

'not,thatmeanswearecreatinganewdocumentandneedtobesureto

'inserttheXMLprocessinginstruction.

IfblnFileExists=Falsethen

'Createthexmlprocessinginstruction.

SetobjPI=objDom.createProcessingInstruction("xml","version='1.0'")

'AppendtheprocessinginstructiontotheXMLdocument.

objDom.insertBeforeobjPI,objDom.childNodes(0)

EndIf

'SavetheXMLdocument.

objDom.savestrXMLFilePath&""&strFileName

'Releaseallofyourobjectreferences.

SetobjDom=Nothing

SetobjRoot=Nothing

SetobjRecord=Nothing

SetobjField=Nothing

SetobjFieldValue=Nothing

SetobjattID=Nothing

SetobjattTabOrder=Nothing

SetobjPI=NothingEnd

Function

'Donotbreakonanerror.

OnErrorResumeNext

'CalltheaddNewContacttoXMLfunction,passinginthephysicalpathto

'savethefiletoandthenamethatyouwishtouseforthefile.

addNewContacttoXML"c:","rolodex.xml"

'Testtoseeifanerroroccurred,ifso,lettheuserknow.

'Otherwise,telltheuserthattheoperationwassuccessful.

Iferr.number<>0then

Response.write("Errorsoccurredwhilesavingyourformsubmission.")

Else

Response.write("Yourformsubmissionhasbeensaved.")

EndIf

%>

如果你已经读过关于"将HTML表单数据保存至XML格式"的文章,你会注意到附加到将HTML数据扩展到XML文件的代码与HTML数据扩展到新文档的代码基本上是一致的。但是这里还是有两个主要的不同点:

'CalltheLoadMethodoftheXMLDOMObject.TheLoadMethodhasa

'booleanreturnvalueindicatingwhetherornotthefilecouldbe

'loaded.Ifthefileexistsandloadsitwillreturntrue,otherwise,

'itwillreturnfalse.

blnFileExists=objDom.Load(strXMLFilePath&""&strFileName)

'Testtoseeifthefileloadedsuccessfully.

IfblnFileExists=TrueThen

'IfthefileloadedsettheobjRootObjectequaltotherootelement

'oftheXMLdocument.

SetobjRoot=objDom.documentElement

Else

'CreateyourrootelementandappendittotheXMLdocument.

SetobjRoot=objDom.createElement("contact")

objDom.appendChildobjRoot

EndIf

本节的代码来自addNewContacttoXML功能。因为我们不可能每次都新建一个文件,所以我们改为保存CONTACT。如果能够LOAD此文件呢,我们则获得了这个XML文档的根元素;如果不能够呢,那么我们就假设它不存在并创建一个新的要元素并将它附加到XML文档上。

另外一个主要区别在于:当我们对文件进行二次检测,是否成功的LOAD,这样我们可以决定是否需要加上一条处理指令。如果文件存在,我们就不需要加上这条指令。但是,如果创建了一个新的文件,那么则一定得加上这条处理指令。

'Checkonceagaintoseeifthefileloadedsuccessfully.Ifitdid

'not,thatmeanswearecreatinganewdocumentandneedtobesureto

'inserttheXMLprocessinginstruction.

IfblnFileExists=Falsethen

'Createthexmlprocessinginstruction.

SetobjPI=objDom.createProcessingInstruction("xml","version='1.0'")

'AppendtheprocessinginstructiontotheXMLdocument.

objDom.insertBeforeobjPI,objDom.childNodes(0)

EndIf

除开以上两点不同之处外,你可以发现保存数据至新文件的代码实际上是与附加新record至存在文件的代码是一样的。我们创建一个新的element,contactCONTAINER,以便能容下每个新添的RECORD。代码将会在FormCollectionoftheRequestObjec中不断重复以创建适合的XML节点并将这些节点值设置得与当前FormField.一样。

如以往一样,我推荐大家复制以上代码至你的服务器上并运行。希望以上举例会对你有所帮助。

【给XML文档添加新 ”records”】相关文章:

得到XML文档大小的方法

XML文档搜索使用小结

包含中文字符的URL编码问题

XML指南——XML数据岛

XML加ASP实现网页“本地化”

XML指南——XML元素

XSL简明教程(6)XSL过滤和查询

使用XSL将XML文档中的CDATA注释输出为HTML文本

气象 XML 数据源应用程序开发指南-内容目录

如何得到XML文档大小

精品推荐
分类导航