Home  >  Article  >  Backend Development  >  Add new "records" to XML document

Add new "records" to XML document

黄舟
黄舟Original
2017-02-11 15:54:381421browse

The example given in this article is 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, then we can add it directly. The use of this technique is similar to creating basic data.

In the previous article, I have demonstrated how to use XMLDOM. So we can jump right into the example of 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 already used this form, just changed the file name, but the code is the same.

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>


We set up this HTML form to handle ADD. ASP. The ASP page here has the function of detecting whether XML. files and ROLODEX.XML exist. If they do exist, ASP appends new entries to the files. If the files do not exist, they need to be created.

 Process Add.asp:

<% 
   &#39;-------------------------------------------------------------------- 
   &#39;The "addNewContacttoXML" Function accepts two parameters. 
   &#39;strXMLFilePath - The physical path where the XML file will be saved. 
   &#39;strFileName - The name of the XML file that will be saved. 
   &#39;-------------------------------------------------------------------- 
   Function addNewContacttoXML(strXMLFilePath, strFileName)  
    &#39;Declare local variables.  
    Dim objDom  
    Dim objRoot  
    Dim objRecord  
    Dim objField 
    Dim objFieldValue  
    Dim objattID  
    Dim objattTabOrder  
    Dim objPI  
    Dim blnFileExists  
    Dim x  
    &#39;Instantiate the Microsoft XMLDOM.  
    Set objDom = server.CreateObject("Microsoft.XMLDOM")  
    objDom.preserveWhiteSpace = True 
    &#39;Call the Load Method of the XMLDOM Object. The Load ethod has a  
    &#39;boolean return value indicating whether or not the file could be  
    &#39;loaded. If the file exists and loads it will return true, otherwise, 
    &#39;it will return false. 
    blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)  
    &#39;Test to see if the file loaded successfully.  
    If blnFileExists = True Then  
     &#39;If the file loaded set the objRoot Object equal to the root element  
     &#39;of the XML document.  
     Set objRoot = objDom.documentElement Else  
     &#39;Create your root element and append it to the XML document.  
     Set objRoot = objDom.createElement("rolodex")  
     objDom.appendChild objRoot 
    End If  
     &#39;Create the new container element for the new record.  
     Set objRecord = objDom.createElement("contact")  
     objRoot.appendChild objRecord  
     &#39;Iterate through the Form Collection of the Request Object. 
     For x = 1 To Request.Form.Count  
      &#39;Check to see if "btn" is in the name of the form element. If it is,  
      &#39;then it is a button and we do not want to add it to the XML  
      &#39;document".  
      If instr(1,Request.Form.Key(x),"btn") = 0 Then  
       &#39;Create an element, "field".  
       Set objField = objDom.createElement("field")  
       &#39;Create an attribute, "id".  
       Set objattID = objDom.createAttribute("id")  
       &#39;Set the value of the id attribute equal the the name of the current  
       &#39;form field.  
       objattID.Text = Request.Form.Key(x)  
       &#39;The setAttributeNode method will append the id attribute to the  
       &#39;field element. objField.setAttributeNode objattID  
       &#39;Create another attribute, "taborder". This just orders the  
       &#39;elements.  
       Set objattTabOrder = objDom.createAttribute("taborder")  
        
       &#39;Set the value of the taborder attribute.  
       objattTabOrder.Text = x  
       &#39;Append the taborder attribute to the field element.  
       &#39;objField.setAttributeNode objattTabOrder  
       &#39;Create a new element, "field_value". 
       Set objFieldValue = objDom.createElement("field_value")  
       &#39;Set the value of the field_value element equal to the value of the  
       &#39;current field in the Form Collection.  
       objFieldValue.Text = Request.Form(x)  
       &#39;Append the field element as a child of the new record container  
       &#39;element, contact. objRecord.appendChild objField  
       &#39;Append the field_value element as a child of the field element. 
       objField.appendChild objFieldValue  
      End If  
     Next  
     &#39;Check once again to see if the file loaded successfully. If it did  
     &#39;not, that means we are creating a new document and need to be sure to  
     &#39;insert the XML processing instruction.  
     If blnFileExists = False then  
      &#39;Create the xml processing instruction.  
      Set objPI = objDom.createProcessingInstruction("xml", "version=&#39;1.0&#39;")  
      &#39;Append the processing instruction to the XML document.  
      objDom.insertBefore objPI, objDom.childNodes(0)  
     End If  
     &#39;Save the XML document. 
     objDom.save strXMLFilePath & "\" & strFileName  
     &#39;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 
    &#39;Do not break on an error. 
    On Error Resume Next 
    &#39;Call the addNewContacttoXML function, passing in the physical path to 
    &#39;save the file to and the name that you wish to use for the file. 
    addNewContacttoXML "c:","rolodex.xml" 
    &#39;Test to see if an error occurred, if so, let the user know. 
    &#39;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 the article about "Saving HTML form data to XML format", you will notice that the append to the HTML data extension The code to the XML file is basically the same as the code to expand the HTML data into the new document. But there are two main differences here:

 &#39;Call the Load Method of the XMLDOM Object. The Load Method has a  
   &#39;boolean return value indicating whether or not the file could be  
   &#39;loaded. If the file exists and loads it will return true, otherwise,  
   &#39;it will return false.  
   blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)  
    
   &#39;Test to see if the file loaded successfully.  
   If blnFileExists = True Then  
    &#39;If the file loaded set the objRoot Object equal to the root element  
    &#39;of the XML document.  
    Set objRoot = objDom.documentElement 
   Else  
    &#39;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 we can't create a new file every time, we save CONTACT instead. If we can load the file, we get the root element of the XML document; if not, we assume it doesn't exist and create a new element and append it to the XML document.

Another main difference is: when we perform a secondary check on the file to see whether the LOAD was successful, we can decide whether we need to add a processing instruction. If the file exists, we don't need to add this directive. However, if a new file is created, this processing instruction must be added.


&#39;Check once again to see if the file loaded successfully. If it did  
  &#39;not, that means we are creating a new document and need to be sure to  
  &#39;insert the XML processing instruction.  
  If blnFileExists = False then  
   &#39;Create the xml processing instruction.  
   Set objPI = objDom.createProcessingInstruction("xml", "version=&#39;1.0&#39;")  
   &#39;Append the processing instruction to the XML document.  
   objDom.insertBefore objPI, objDom.childNodes(0)  
  End If

Apart from the above two differences, 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, contact CONTAINER, to accommodate each newly added RECORD. The code will be iterated in the Form Collection of the Request Object to create the appropriate XML nodes and set the node values ​​to be the same as the current Form Field.

The above is the content of adding new "records" to the XML document. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn