Add "records" to the XML document"

Source: Internet
Author: User
Tags processing instruction

The example in this article is similar to saving HTML format data to XML. In the past, when a table is submitted, we usually create a new document. Now, as long as the document already exists, simply add it. The use of this technology is similar to the creation of basic data.

PreviousArticleI have demonstrated how to use xmldom. Therefore, we can directly enter the example in this article.

The first thing we need to consider is to add an HTML form for the new "record. In the "Save HTML form data to XML" example, we have used this form, but only changed the file name,CodeIs the same.

Addcontact.html: Copy code The Code is as follows: <HTML>
<Head>
<Title> Contact Information </title>
</Head>
<Body>
<Form action = "processadd. asp" method = "Post">
<H3> enter your contact information 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> Email:
<Input type = "text" id = "email" name = "email"> <br>
<Input type = "Submit" id = "btnsub" name = "btnsub" value = "Submit"> <br>
</Form>
</Body>
</Html>

We set this HTML form to process add. ASP. The ASP page is used to check whether the XML. File and Rolodex. xml exist. If they do exist, ASP will append a new entry to the file. If the file does not exist, you need to create it.

Process Add. asp: Copy code The Code is as follows: <%
'--------------------------------------------------------------------
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)
'Descare 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
'Boolean return value indicating whether or not the file cocould 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.doc umentelement 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 name of the current
'Form field.
Objattid. Text = request. Form. Key (X)
'The setattributenode method will append the ID attribute to
'Field element. objfield. setattributenode objattid
'Create another attribute, "taborder". This just orders
'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
'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
'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
'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
%>

If you have read an article about "Saving HTML form data to XML format, you will notice that the Code appended to the HTML data extended to the XML file is basically the same as the Code extended to the new document. However, there are two main differences:

'Call the load method of the xmldom object. The load method has
'Boolean return value indicating whether or not the file cocould 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.doc umentelement
Else

'Create your root element and append it to the XML document.
Set objroot = objdom. createelement ("Contact ")
Objdom. appendchild objroot
End if

The code in this section comes from the addnewcontacttoxml function. Because it is impossible for us to create a new file every time, we save the contact. If you can load this file, we get the root element of this XML file. If not, assume that it does not exist and creates a new element and attaches it to the XML document.

Another major difference is: When we perform a secondary check on the file and check whether the load is successful, we can decide whether to add a processing command. If the file exists, we do not need to add this command. However, if a new file is created, you must add this processing instruction.

'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
'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

Apart from the two differences above, you can find that the code for saving data to a new file is actually the same as the code for appending a new record to an existing file. We create a new element and contact iner to accommodate each newly added record. The code will repeatedly create appropriate XML nodes in Form collection of the Request objec and set these node values to the same as the current form field.

As before, I recommend that you copy the above Code to your server and run it. I hope the above examples will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.