The examples given in this article are similar to saving HTML format data to XML. In the past, when the form was submitted, we usually created a new document. Now, as long as the document already exists, we can just add it. This technique is used similarly to creating basic data.
In the previous article, I have demonstrated how to use XMLDOM. Therefore, we can go directly to the examples in this article.
The first thing we need to consider is the HTML form we will use to add new "records". In the "Save HTML form data to XML" example we have used this form, just changed the file name, but the code is the same.
:
<html>
<head>
<title> Contact Information </title>
</head>
<body>
<form action="" method="post">
<h3>Enter your contact information</h3>
First Name:
<input type="text" name="firstName"><br> Last Name:
<input type="text" name="lastName"><br> Address #1:
<input type="text" name="address1"><br> Address #2:
<input type="text" name="address2"><br> Phone Number:
<input type="text" name="phone"><br> E-Mail:
<input type="text" name="email"><br>
<input type="submit" name="btnSub" value="Submit"><br>
</form>
</body>
</html>
We set this HTML form to handle ADD. ASP. The ASP page here has the function of detecting XML files and whether they exist. If they do exist, ASP will attach a new entry to the file, and if the file does not exist, it will need to be created.
Process :
<%
'--------------------------------------------------------------------
'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 = ("")
= 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 = (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 = Else
'Create your root element and append it to the XML document.
Set objRoot = ("rolodex")
objRoot
End If
'Create the new container element for the new record.
Set objRecord = ("contact")
objRecord
'Iterate through the Form Collection of the Request Object.
For x = 1 To
'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,(x),"btn") = 0 Then
'Create an element, "field".
Set objField = ("field")
'Create an attribute, "id".
Set objattID = ("id")
'Set the value of the id attribute equal the the name of the current
'form field.
= (x)
'The setAttributeNode method will append the id attribute to the
'field element. objattID
'Create another attribute, "taborder". This just orders the
'elements.
Set objattTabOrder = ("taborder")
'Set the value of the taborder attribute.
= x
'Append the taborder attribute to the field element.
' objattTabOrder
'Create a new element, "field_value".
Set objFieldValue = ("field_value")
'Set the value of the field_value element equal to the value of the
'current field in the Form Collection.
= (x)
'Append the field element as a child of the new record container
'element, contact. objField
'Append the field_value element as a child of the field element.
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 = ("xml", "version='1.0'")
'Append the processing instruction to the XML document.
objPI, (0)
End If
'Save the XML document.
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:",""
'Test to see if an error occurred, if so, let the user know.
'Otherwise, tell the user that the operation was successful.
If <> 0 then
("Errors occurred while saving your form submission.")
Else
("Your form submission has been saved.")
End If
%>
If you have read the article about "Save HTML form data to XML format", you will notice that the code attached to extending HTML data to XML files is basically the same as the code that extends HTML data to new documents. But there are still two main differences:
'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 = (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 =
Else
'Create your root element and append it to the XML document.
Set objRoot = ("contact")
objRoot
End If
The code in this section comes from the addNewContacttoXML function. Because we can't create a new file every time, we save CONTACT instead. If we can LOAD this file, we get the root element of the XML document; if we can't, we assume that it does not exist and create a new element to be added to the XML document.
Another major difference is: when we perform secondary detection of the file, whether it is successful is LOAD, so we can decide whether we need to add a processing instruction. If the file exists, we don't need to add this command. However, if a new file is created, this processing instruction must be added.
'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 = ("xml", "version='1.0'")
'Append the processing instruction to the XML document.
objPI, (0)
End If
Apart from the above two differences, you can find that the code that saves data to a new file is actually the same as the code that attaches a new record to an existing file. We create a new element, contact CONTAINER, so that we can accommodate each newly added RECORD. The code will be repeated in Form Collection of the Request Object to create appropriate XML nodes and set these node values to the same as the current Form Field.
As always, I recommend that you copy the above code to your server and run it. Hope the above examples will be helpful to you.
In the previous article, I have demonstrated how to use XMLDOM. Therefore, we can go directly to the examples in this article.
The first thing we need to consider is the HTML form we will use to add new "records". In the "Save HTML form data to XML" example we have used this form, just changed the file name, but the code is the same.
:
Copy the codeThe code is as follows:
<html>
<head>
<title> Contact Information </title>
</head>
<body>
<form action="" method="post">
<h3>Enter your contact information</h3>
First Name:
<input type="text" name="firstName"><br> Last Name:
<input type="text" name="lastName"><br> Address #1:
<input type="text" name="address1"><br> Address #2:
<input type="text" name="address2"><br> Phone Number:
<input type="text" name="phone"><br> E-Mail:
<input type="text" name="email"><br>
<input type="submit" name="btnSub" value="Submit"><br>
</form>
</body>
</html>
We set this HTML form to handle ADD. ASP. The ASP page here has the function of detecting XML files and whether they exist. If they do exist, ASP will attach a new entry to the file, and if the file does not exist, it will need to be created.
Process :
Copy the codeThe 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)
'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 = ("")
= 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 = (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 = Else
'Create your root element and append it to the XML document.
Set objRoot = ("rolodex")
objRoot
End If
'Create the new container element for the new record.
Set objRecord = ("contact")
objRecord
'Iterate through the Form Collection of the Request Object.
For x = 1 To
'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,(x),"btn") = 0 Then
'Create an element, "field".
Set objField = ("field")
'Create an attribute, "id".
Set objattID = ("id")
'Set the value of the id attribute equal the the name of the current
'form field.
= (x)
'The setAttributeNode method will append the id attribute to the
'field element. objattID
'Create another attribute, "taborder". This just orders the
'elements.
Set objattTabOrder = ("taborder")
'Set the value of the taborder attribute.
= x
'Append the taborder attribute to the field element.
' objattTabOrder
'Create a new element, "field_value".
Set objFieldValue = ("field_value")
'Set the value of the field_value element equal to the value of the
'current field in the Form Collection.
= (x)
'Append the field element as a child of the new record container
'element, contact. objField
'Append the field_value element as a child of the field element.
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 = ("xml", "version='1.0'")
'Append the processing instruction to the XML document.
objPI, (0)
End If
'Save the XML document.
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:",""
'Test to see if an error occurred, if so, let the user know.
'Otherwise, tell the user that the operation was successful.
If <> 0 then
("Errors occurred while saving your form submission.")
Else
("Your form submission has been saved.")
End If
%>
If you have read the article about "Save HTML form data to XML format", you will notice that the code attached to extending HTML data to XML files is basically the same as the code that extends HTML data to new documents. But there are still two main differences:
'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 = (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 =
Else
'Create your root element and append it to the XML document.
Set objRoot = ("contact")
objRoot
End If
The code in this section comes from the addNewContacttoXML function. Because we can't create a new file every time, we save CONTACT instead. If we can LOAD this file, we get the root element of the XML document; if we can't, we assume that it does not exist and create a new element to be added to the XML document.
Another major difference is: when we perform secondary detection of the file, whether it is successful is LOAD, so we can decide whether we need to add a processing instruction. If the file exists, we don't need to add this command. However, if a new file is created, this processing instruction must be added.
'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 = ("xml", "version='1.0'")
'Append the processing instruction to the XML document.
objPI, (0)
End If
Apart from the above two differences, you can find that the code that saves data to a new file is actually the same as the code that attaches a new record to an existing file. We create a new element, contact CONTAINER, so that we can accommodate each newly added RECORD. The code will be repeated in Form Collection of the Request Object to create appropriate XML nodes and set these node values to the same as the current Form Field.
As always, I recommend that you copy the above code to your server and run it. Hope the above examples will be helpful to you.