Advertisement
6_2008-2009 Classes/ Frameworks/ OOP #209700

___XML as Data Source Part 1 of 3 (Updated)

Learn whatever you ever wanted to know about XML and ASP. This article starts from the very basics and ultimately creates something useful. Written in an easy to understand language. Unlike other articles which contain a lot of technical jargon, this article rather emphasises on showing by doing. Give it a good reading. Comments and suggestions are always welcome. Thanks.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
<blockquote>
 <hr size="1" color="#006600">
 <p align="center"><b><i><font color="#0000FF" face="Times New Roman" size="6">XML as Data Source</font></i></b></p>
 <p align="center"><b><font face="Verdana" size="2">Written by <a href="mailto:nascentsoft@yahoo.com">Amit
 Mathur</a>, India on 6th Dec., 2002</font></b></p>
 <p align="center"><b><i><font size="4" color="#0000FF"><span style="background-color: #CCCCCC">Part
 1 of 3<br>
 </span></font></i></b></p>
 <p align="center"><b><i><font face="Times New Roman" size="5">Explore the
 exciting world of XML !</font></i></b></p>
 <p align="left"><font face="Verdana" size="2">This is first of a series of 3
 articles that I will write to illustrate the use of XML as Data Source.&nbsp; I have divided this tutorial
 into 3 chunks. After this tutorial, you will be able to manipulate and play
 with XML Data files in the same way as you do with MS Access. In addition to
 that, there are certain advantages of using XML, over MS Access. I introduce
 these advantages in the text, as we proceed on. The article has been organized
 in the increasing order of difficulty.</font></p>
 <p align="center"><br>
 <font face="Verdana"><b>Section 1</b></font><b><font face="Arial Black"><br>
 </font><font face="Verdana" color="#FF0000">Retrieving XML using ASP</font></b></p>
 <table border="0" cellpadding="0" cellspacing="0" width="90%">
  <tr>
   <td width="3%" valign="top"><font face="Verdana" size="6"><b>F</b></font></td>
   <td width="97%" valign="top"><font face="Verdana" size="2">or most of the net based organizations, the data stored in their databases are their sole property. How this data can be organized more effectively and efficiently using XML, is the theme of this article.&nbsp;</font></td>
  </tr>
 </table>
 <p align="left"><font face="Verdana" size="2">When you create a database in MS Access, even if it is empty, it takes a lot of space just for structure. Right ? Why waste this much space just for structure when you can store information about at least a hundred members of your site in the same space. When you store data as XML, it is stored in the form of a simple text file with the extension .XML, along with a few tags. For the time being, I will not go in the detailed theory of explaining the
 intricacies of XML, but rather concentrate on how to access and manipulate it using ASP.<br>
 <br>
 Before we begin, what all is required for your web server to be able to serve XML. For the Server Side XML Processing, IE 5.0+ must be installed on the server. This is because we need XML DOM (XML Document Object Model), which is basically an XML Parser, which is provided as a component of IE5.<br>
 <br>
 <br>
 Now we are ready to write our scripts. We need two things.&nbsp;</font></p>
 <ul>
  <li>
   <p align="left"><font face="Verdana" size="2"><b>Data Source</b></font></li>
  <li>
   <p align="left"><font face="Verdana" size="2"><b>ASP Script to manipulate the data.</b></font></li>
 </ul>
 <p align="left"><font face="Verdana" size="2"><b>Let us first create a data
 source</b>. Suppose we want to maintain a resource repository in which we want to include a brief description of the resource, its link on the web and its link text. The Sample XML data source for this can be:</font></p>
 <blockquote>
  <p align="left"><code><font face="Verdana" size="2" color="#FF0000"><b> (Contents of File Resource.XML)</b><br>
  </font><font color="#006600" face="Verdana" size="2"><br>
 &lt;?xml version="1.0" ?>&nbsp;<br>
 &lt;Resource_Repository><br>
  &nbsp;&nbsp;&nbsp; &lt;Resource><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;text><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This program allows you to encrypt your HTML Source Codes so that no one can 			steal&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and Copy Paste them in their own site.<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/text>&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;link><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;url><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  ftp://ftp.cedium.net/internet/htmlcrypt/encryptHTML.exe<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/url>&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;linetext><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  HTML Source Code Encryption<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/linetext>&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/link><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Size&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  736 KB<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Size>&nbsp;<br>
  &nbsp;&nbsp;&nbsp; &lt;/Resource><br>
  <br>
  &nbsp;&nbsp;&nbsp; &lt;Resource><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;text><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DOS Based Eliza Program. Eliza is a Computer program that can talk to you like a 		human&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  being. Makes use of Artificial Intelligence.<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/text>&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;link><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;url><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  http://hps.elte.hu/~gk/Eliza/ELIZA.EXE<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/url>&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;linetext><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  Eliza Chatterbot<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/linetext>&nbsp;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/link><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Size&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  36 KB<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Size>&nbsp;<br>
  &nbsp;&nbsp;&nbsp; &lt;/Resource><br>
 &lt;/Resource_Repository></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Now we write a <b> script to access this data</b> and display it on the web page. I result will be generated in a very simple format, to make the ASP Code simple to understand.<br>
 <br>
 To begin with, we need to <b> create an XML DOM object</b> on the server. This can be done with a line as simple as :</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" face="Verdana" size="2">    &lt;% Set objXML = Server.CreateObject("Microsoft.XMLDOM") %></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Now we need to <b> give the path of our data
 source</b>. So add the following line to the code:</font></p>
 <blockquote>
  <p align="left"><code><font face="Verdana" color="#006600" size="2">    &lt;%&nbsp;<br>
	objXML.Load (Server.MapPath("Resource.xml"))<br>
  <br>
	If objXML.parseError.errorCode &lt;> 0 Then<br>
		Response.Write "&lt;p>&lt;font color=red>Error loading the Resource file.&lt;/font>&lt;/p>"<br>
		Response.End<br>
	End If<br>
	%></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">If the XML DOM Parser throws any error, then we catch that and display the error message.<br>
 <br>
 We want to display the list of all the resources in the file Resource.xml. We know that the resources are enclosed within &lt;Resource> tag. So, the next step is to
 <b> create a list of all the objects</b> under the tag &lt;Resource>.&nbsp;</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" face="Verdana" size="2">    &lt;%<br>
    Set objLst = objXML.getElementsByTagName("Resource")<br>
    %></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">The Length property of object list indicates the
 <b> number of items in the list</b>. For example, in our example of Resource.xml, there are two records under the &lt;Resource> tag. So, objLst.Length shows the value 2, when displayed.<br>
 </font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" face="Verdana" size="2">    &lt;%<br>
    Response.write "There are " &amp; objLst.Length &amp; " resources on this site."<br>
	%></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2"><br>
 This is quite interesting upto now. What about accessing individual sub-items ? Suppose we just want to display the URLs of all the resources in our 'database', how can we do that ?&nbsp;<br>
 <br>
 Create one more object, now referring to the items inside the object list. The
 <b> sub-items can be accessed </b> by using childNodes(index). See this code for further understanding :&nbsp;</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" face="Verdana" size="2">    &lt;%&nbsp;<br>
    ' for each resource in the list<br>
    For i = 0 To objLst.Length - 1&nbsp;<br>
  <br>
    	Set subLst = objLst.item(i)<br>
  <br>
		Response.write "&lt;P>" &amp; vbcrlf&nbsp;<br>
    	Response.Write "&lt;a href='" &amp; subLst.childNodes(1).childNodes(0).Text &amp; "'>" _<br>
    	 &amp; subLst.childNodes(1).childNodes(0).Text &amp; "&lt;/a>"&nbsp;<br>
    Next<br>
    %></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">A careful look at the structure of the XML document that we had created earlier reveals that &lt;URL> is inside &lt;Link> which is second element of the &lt;Resource> (first being &lt;text>). If we start our indexing from 0 (Zero), then &lt;Link> is the first element of &lt;Resource> and &lt;URL> is the Zeroeth element inside &lt;Link>. That's why I have written the line:</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" face="Verdana" size="2">    subLst.childNodes(1).childNodes(0).Text</font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">which is nothing but &lt;URL>. Re-read the previous paragraph if you are still not clear and I am sure you will get it soon.<br>
 <br>
 So, the <b>final source code</b> of the ASP file we have created for displaying the URL of all the resources in our database is:</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" face="Verdana" size="2">    &lt;%&nbsp;<br>
    ' creating an object of XMLDOM<br>
    Set objXML = Server.CreateObject("Microsoft.XMLDOM")&nbsp;<br>
  <br>
    ' Locating our XML database<br>
    objXML.Load (Server.MapPath("Resource.xml"))<br>
  <br>
    ' checking for error<br>
    If objXML.parseError.errorCode &lt;> 0 Then<br>
		Response.Write "&lt;p>&lt;font color=red>Error loading the Resource file.&lt;/font>&lt;/p>"<br>
		Response.End<br>
    End If<br>
  <br>
    ' referring to Resource&nbsp;<br>
    Set objLst = objXML.getElementsByTagName("Resource")<br>
    Response.write "There are " &amp; objLst.Length &amp; " resources on this site ."<br>
  <br>
    ' for each resource<br>
    For i = 0 To objLst.Length - 1&nbsp;<br>
  <br>
     ' refer to sub-item<br>
     Set subLst = objLst.item(i)<br>
  <br>
     'display the URL<br>
     Response.Write "&lt;a href='" &amp; subLst.childNodes(1).childNodes(0).Text &amp; "'>" _&nbsp;<br>
      &amp; subLst.childNodes(1).childNodes(0).Text &amp; "&lt;/a>"<br>
    Next&nbsp;<br>
    %></font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Of course, you would need to add all those formatting HTML tags to make this list more attractive. But the core remains the same.<br>
 <br>
 <br>
 <b>Conclusion:<br>
 </b><br>
 In this article, we learned how to retrieve the 'records' from XML 'database'. For complete Database related operations, we must know how to insert, update and delete the records from the Scripts. In the next article, we will see how to perform these operations as well.</font></p>
 <hr size="1" color="#006600">
 <p align="center"><font face="Verdana"><b>Section 2</b></font><b><font face="Arial Black"><br>
 </font><font face="Verdana" color="#FF0000">Saving Information in XML Files
 Using ASP</font></b></p>
 <p align="center">&nbsp;</p>
 <table border="0" cellpadding="0" cellspacing="0" width="90%">
  <tr>
   <td width="3%" valign="top"><font face="Verdana" size="6"><b>N</b></font></td>
   <td width="97%" valign="top"><font face="Verdana" size="2">ow when you
    know how to retrieve data from XML files, the next thing you have to
    learn is, <i>how to create these XML files from ASP. </i>In this Section
    of
    the tutorial, I demonstrate how to store data in XML files<i>.</i></font></td>
  </tr>
 </table>
 <p align="left"><font face="Verdana" size="2"><b>Advantages of storing data in
 XML, as compared to in MS Access are:</b></font></p>
 <ul>
  <li>
   <p align="left"><font face="Verdana" size="2">XML is <b> platform
   independent</b>.
   You can export your data to Non-Windows OS also.</font></li>
  <li>
   <p align="left"><font face="Verdana" size="2">XML data <b> contains semantics</b>
   along with it. This means that you can associate a name and property with
   data. Further, it is <b> Hierarchical data</b>. So, it gets all those advantages
   of storing data in the form of Trees, <i>in-built</i> in it !</font></li>
  <li>
   <p align="left"><font face="Verdana" size="2">XML consumes <b> much-much less
   space</b> than MS Access data.</font></li>
  <li>
   <p align="left"><font face="Verdana" size="2">XML data can be used in <b> EDI
   </b>
   (Electronic Data Interchange). It can also be used in presenting the data
   in different formats. Like publishing the same data in PDF, HTML, DOC, PS
   ... formats.</font></li>
 </ul>
 <p align="left"><font face="Verdana" size="2">There are many more advantages
 of using XML. Try google to find out more (rather bookish) advantages. That's
 not more important for us. We continue with our business - Saving data (or
 information, if it really means!) to XML Files.</font></p>
 <p align="left"><font face="Verdana" size="2">In this exercise, three files
 are involved:</font></p>
 <ul>
  <li>
   <p align="left"><b><font face="Verdana" size="2">HTML Form File</font></b></li>
  <li>
   <p align="left"><b><font face="Verdana" size="2">ASP Processing Script</font></b></li>
  <li>
   <p align="left"><b><font face="Verdana" size="2">XML Data File</font></b></li>
 </ul>
 <p align="left"><font face="Verdana" size="2"><b>HTML Form File:<br>
 <br>
 </b>Let us first create the HTML Source file, the easiest of three.&nbsp;The
 screen looks something like this.&nbsp;</font></p>
 <center>
 <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#000000" bgcolor="#CCCCCC">
  <tr>
   <td width="100%">
    <p align="center"><b><br>
    Add Resource To Database</b></p>
    <form method="POST">
     <blockquote>
      <p><br>
      Enter the information about the resource in the form below :&nbsp;</p>
     </blockquote>
     <table border="0" cellpadding="0" cellspacing="0" width="80%">
      <tr>
       <td width="50%">
        <blockquote>
         <p>Resource Title</p>
        </blockquote>
       </td>
       <td width="50%">
        <blockquote>
         <p><input type="text" name="title" size="20"></p>
        </blockquote>
       </td>
      </tr>
      <tr>
       <td width="50%">
        <blockquote>
         <p>Resource URL</p>
        </blockquote>
       </td>
       <td width="50%">
        <blockquote>
         <p><input type="text" name="link" size="20"></p>
        </blockquote>
       </td>
      </tr>
      <tr>
       <td width="50%">
        <blockquote>
         <p>Resource Description</p>
        </blockquote>
       </td>
       <td width="50%">
        <blockquote>
         <p><textarea rows="2" name="description" cols="20"></textarea></p>
        </blockquote>
       </td>
      </tr>
      <tr>
       <td width="50%">
        <blockquote>
         <p>Size&nbsp;</p>
        </blockquote>
       </td>
       <td width="50%">
        <blockquote>
         <p><input type="text" name="size" size="20"></p>
        </blockquote>
       </td>
      </tr>
     </table>
     <blockquote>
      <p><input type="submit" value="Add Resource" name="submit"> (This
      won't work here. To see a demo, try the link provided at the end)</p>
     </blockquote>
    </form>
    <p align="center"><b><font color="#FF0000">Sample Data Entry Screen</font></b></td>
  </tr>
 </table>
 </center>
 <p align="left"><font face="Verdana" size="2">The various form field names are
 given as: title, url, description, size and Submit, in the top-down order as
 shown above.</font></p>
 <p align="left"><font face="Verdana" size="2">Now we write an ASP Script, that
 will store this data in the XML file. In the current section, we concentrate
 on just saving the data in a file. If the file already exists, then it will be
 deleted and then again, contents will be written into it. In the next section,
 we will see how to append data to an existing file.</font></p>
 <p align="left"><font face="Verdana" size="2">So, we proceed further. As done
 in previous section, we first <b>create an object of Microsoft.XMLDOM</b>.</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600">&lt;%<br>
  Set objXML = server.CreateObject(&quot;Microsoft.XMLDOM&quot;)&nbsp;<br>
  objXML.preserveWhiteSpace = True<br>
  %&gt;</font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Here the second line indicates
 that we want to preserve the White Spaces in the file. Now we <b>create the
 root element of the document and attach it with the XML document</b>. Look at
 the XML document structure given at the beginning of this article, to find
 that the root element is Resource_Repository.</font></p>
 <blockquote>
  <p align="left"><font color="#006600"><code>&lt;%<br>
  Set objRoot = objXML.createElement(&quot;Resource_Repository&quot;)&nbsp;<br>
  objXML.appendChild objRoot<br>
  %&gt;</code></font></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Now we <b>create a new element</b>
 and place it under Resource_Repository. A quick glance at the structure
 reveals that the direct child of Resource_Repository is Resource. The
 immediate child of Resource is &lt;text&gt;, which stores the resource
 description. So, three steps are involved in creating a child.</font></p>
 <ul>
  <li>
   <p align="left"><font face="Verdana" size="2"><b>Create the element</b></font></li>
  <li>
   <p align="left"><font face="Verdana" size="2"><b>Assign value to the
   element (optional)</b></font></li>
  <li>
   <p align="left"><font face="Verdana" size="2"><b>Make this new element
   child of some other element.</b></font></li>
 </ul>
 <p align="left"><font face="Verdana" size="2">The following code demonstrates
 the first and last of above steps.</font></p>
 <blockquote>
  <p align="left"><font color="#006600"><code>&lt;%<br>
  'Create an element, "Resource".<br>
  Set objResource = objXML.createElement("Resource")<br>
  <br>
  ' Make &lt;Resource> a child of Root<br>
  objRoot.appendChild objResource<br>
  %&gt;<br>
  </code></font></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Now we create another element
 &lt;text&gt; which stores the description of the resource. We follow all the 3
 steps mentioned earlier in this case. Try to figure them out in the following
 code:</font></p>
 <blockquote>
  <p align="left"><font color="#006600"><code>&lt;%<br>
  'Create a new element, "text". This stores Description of the resource<br>
  Set objText = objXML.createElement("text")<br>
  <br>
  ' Assign the description to the newly created object<br>
  objText.Text = Request.Form("description")<br>
  <br>
  ' Make Text (description) a child of Resource<br>
  objResource.appendChild objText<br>
  %&gt;</code></font></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Easy, isn't it ? Now look at the
 following code and observe the symmetry. Please note that this code is written
 in accordance with the structure of XML document (Resource.xml) defined at the
 start of the article.</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" size="2">&lt;%<br>
 ' create another child of &lt;Resource> ie &lt;link><br>
  Set objLink = objXML.createElement("Link")<br>
  <br>
  ' make &lt;link> a child of &lt;Resouce><br>
  objResource.appendChild objLink<br>
  <br>
  ' create one more element - &lt;url>&nbsp;<br>
  Set objUrl = objXML.createElement("url")<br>
  <br>
  ' capture URL value from the form and assign it to<br>
  ' the newly created &lt;url> element<br>
  objUrl.Text = Request.Form("url")<br>
  <br>
  ' make &lt;url> a child of &lt;link><br>
  objLink.appendChild objUrl<br>
  <br>
  ' create a &lt;linetext> element, assign it a value&nbsp;<br>
  ' and make it a child of &lt;link><br>
  Set objLineText = objXML.createElement("linetext")<br>
  objLineText.Text = Request.Form("title")<br>
  objLink.appendChild objLineText<br>
  <br>
  <br>
  ' and finally create an element &lt;Size> and make<br>
  ' it a child of &lt;Resource><br>
  Set objSize = objXML.createElement("Size")<br>
  objSize.Text = Request.Form("size")<br>
  objResource.appendChild objSize<br>
  %&gt;</font></code></p>
 </blockquote>
 <p align="left"><code><font face="Verdana" size="2">Now we are done with the
 structure of the document. The next step is to <b>create a Processing
 Instruction (PI) indicating the XML file version</b>, which appears as the
 first line of any XML document.</font></code></p>
 <blockquote>
  <p align="left"><code><font size="2" color="#006600">&lt;%<br>
 'Create the xml processing instruction.<br>
  Set objPI = objXML.createProcessingInstruction("xml", "version='1.0'")<br>
  <br>
  'Append the processing instruction to the XML document.<br>
  objXML.insertBefore objPI, objXML.childNodes(0)<br>
  %&gt;</font></code></p>
 </blockquote>
 <p align="left"><code><font face="Verdana" size="2">And as a penultimate step,
 <b>save the XML file</b>.</font></code></p>
 <blockquote>
  <p align="left"><code><font size="2"><font color="#006600">&lt;%<br>
  'Save the XML document.<br>
  objXML.save strXMLFilePath &amp; "\" &amp; strFileName<br>
  %&gt;</font></font></code></p>
 </blockquote>
 <p align="left"><code><font face="Verdana" size="2">Assuming strXMLFilePath
 and strFileName to be two variables. And finally, <b>free all the objects</b>.</font></code></p>
 <blockquote>
  <p align="left"><code><font color="#006600" size="2">&lt;%<br>
  'Release all of your object references.<br>
  Set objXML = Nothing<br>
  Set objRoot = Nothing<br>
  Set objResource = Nothing<br>
  Set objText = Nothing<br>
  Set objLink = Nothing<br>
  Set objUrl = Nothing<br>
  Set objLineText = Nothing<br>
  Set objSize = Nothing<br>
  %&gt;</font></code></p>
 </blockquote>
 <p align="left"><code><font face="Verdana" size="2">This concludes our
 discussion of saving data to XML files. Before we wrap-up, here is the <b>complete
 code</b>, which is well formatted in the form of a function.</font></code></p>
 <blockquote>
  <p align="left"><code><font size="2" color="#006600">&lt;%<br>
  <br>
  '--------------------------------------------------------------------<br>
  'The "SaveFormData" Function accepts two parameters.<br>
  'strXMLFilePath - The physical path where the XML file will be saved.<br>
  'strFileName - The name of the XML file that will be saved.<br>
  '--------------------------------------------------------------------<br>
  <br>
  Function SaveFormData(strXMLFilePath, strFileName)<br>
  <br>
 'Declare local variables.<br>
 Dim objXML<br>
 Dim objRoot<br>
 Dim objField<br>
 Dim objFieldValue<br>
 Dim objattID<br>
 Dim objattTabOrder<br>
 Dim objPI<br>
 Dim x<br>
  <br>
  <br>
 'Instantiate the Microsoft XMLDOM.<br>
 Set objXML = server.CreateObject("Microsoft.XMLDOM")<br>
 objXML.preserveWhiteSpace = False<br>
  <br>
  <br>
 'Create your root element and append it to the XML document.<br>
 Set objRoot = objXML.createElement("Resource_Repository")<br>
 objXML.appendChild objRoot<br>
  <br>
  <br>
  <br>
  'Create an element, "Resource".<br>
  Set objResource = objXML.createElement("Resource")<br>
  <br>
  ' Make &lt;Resource> a child of Root<br>
  objRoot.appendChild objResource<br>
  <br>
  'Create a new element, "text". This stores Description of the resource<br>
  Set objText = objXML.createElement("text")<br>
  <br>
  ' Assign the description to the newly created object<br>
  objText.Text = Request.Form("description")<br>
  <br>
  ' Make Text (description) a child of Resource<br>
  objResource.appendChild objText<br>
  <br>
  <br>
  ' create another child of &lt;Resource> ie &lt;link><br>
  Set objLink = objXML.createElement("Link")<br>
  <br>
  ' make &lt;link> a child of &lt;Resouce><br>
  objResource.appendChild objLink<br>
  <br>
  ' create one more element - &lt;url>&nbsp;<br>
  Set objUrl = objXML.createElement("url")<br>
  <br>
  ' capture URL value from the form and assign it to<br>
  ' the newly created &lt;url> element<br>
  objUrl.Text = Request.Form("url")<br>
  <br>
  ' make &lt;url> a child of &lt;link><br>
  objLink.appendChild objUrl<br>
  <br>
  ' create a &lt;linetext> element, assign it a value&nbsp;<br>
  ' and make it a child of &lt;link><br>
  Set objLineText = objXML.createElement("linetext")<br>
  objLineText.Text = Request.Form("title")<br>
  objLink.appendChild objLineText<br>
  <br>
  <br>
  ' and finally create an element &lt;Size> and make<br>
  ' it a child of &lt;Resource><br>
  Set objSize = objXML.createElement("Size")<br>
  objSize.Text = Request.Form("size")<br>
  objResource.appendChild objSize<br>
  <br>
  <br>
  'Create the xml processing instruction.<br>
  Set objPI = objXML.createProcessingInstruction("xml", "version='1.0'")<br>
  <br>
  'Append the processing instruction to the XML document.<br>
  objXML.insertBefore objPI, objXML.childNodes(0)<br>
  <br>
  <br>
  'Save the XML document.<br>
  objXML.save strXMLFilePath &amp; "\" &amp; strFileName<br>
  <br>
  <br>
  'Release all of your object references.<br>
  Set objXML = Nothing<br>
  Set objRoot = Nothing<br>
  Set objResource = Nothing<br>
  Set objText = Nothing<br>
  Set objLink = Nothing<br>
  Set objUrl = Nothing<br>
  Set objLineText = Nothing<br>
  Set objSize = Nothing<br>
  End Function<br>
  <br>
  <br>
  'Do not break on an error.<br>
  On Error Resume Next<br>
  <br>
  <br>
  'Call the SaveFormData function, passing in the physical path to&nbsp;<br>
  'save the file to and the name that you wish to use for the file.<br>
  <b>  SaveFormData "C:\Inetpub\wwwroot\save\data","Resource.xml"</b><br>
  <br>
  <br>
  'Test to see if an error occurred, if so, let the user know.<br>
  'Otherwise, tell the user that the operation was successful.<br>
  If err.number &lt;> 0 then<br>
   Response.write("Errors occurred while saving data.")<br>
  Else<br>
   Response.write("&lt;center>&lt;h2>Data Entry Successful&lt;/h2>&lt;P>The resource has been successfully added in the database.&lt;/center> ")<br>
  End If<br>
  %><br>
  </font></code></p>
 </blockquote>
 <p align="left"><font size="2" face="Verdana">Download the file associated
 with the article and test the code on your machine. Do not forget to change
 the path to which the file has to be saved. (This line has been shown in
 strong fonts in above code listing).</font></p>
 <hr size="1" color="#006600">
 <p align="center"><font face="Verdana"><b>Section 3</b></font><b><font face="Arial Black"><br>
 </font><font face="Verdana" color="#FF0000">Appending data to Existing XML Files&nbsp;</font></b></p>
 <table border="0" cellpadding="0" cellspacing="0" width="80%">
  <tr>
   <td width="1%" valign="top"><font face="Verdana" size="6"><b>I</b></font></td>
   <td width="101%" valign="top"><p align="left"><font size="2" face="Verdana">n
    Section 2, we saw how we can save form data to XML files. But if the
    file already existed, then it used to get replaced with the new ones.
    Most of the times we want to append data, i.e., add new data to the
    existing ones. In this Section of the tutorial, we look at how we can do
    so.</font></p>
   </td>
  </tr>
 </table>
 <p align="left"><font size="2" face="Verdana">The major difference between
 appending and creating new file is the Processing Instruction (PI) mentioned
 in the earlier section. When we append data to an existing file, we do not add
 PI to it (obviously, since it is already there!).</font></p>
 <p align="left"><font size="2" face="Verdana">So, we first need to <b>check
 whether the file already exists</b>. This can be done with the following
 simple code:</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" size="2">&lt;%<br>
  blnFileExists = objXML.Load(strXMLFilePath &amp; &quot;\&quot; &amp;
  strFileName)<br>
  %&gt;</font></code></p>
 </blockquote>
 <p align="left"><font size="2" face="Verdana">If the file already exists, then
 we need to<b> set objRoot to the already existing Root element</b> (Resource_Repository).
 If the file does not exist, we create a new root element and attach it to XML
 document, as we did in Section 2.</font></p>
 <blockquote>
  <p align="left"><code><font color="#009900" size="2">&lt;%<br>
  'Test to see if the file loaded successfully.&nbsp;<br>
  If blnFileExists = True Then&nbsp;<br>
  'If the file loaded set the objRoot Object equal to the root element 'of the
  XML document.&nbsp;<br>
  Set objRoot = objXML.documentElement&nbsp;<br>
  Else<br>
  &nbsp;'Create your root element and append it to the XML document.<br>
  Set objRoot = objXML.createElement(&quot;Resource_Repository&quot;)<br>
  objXML.appendChild objRoot&nbsp;<br>
  End If<br>
  %&gt;</font></code></p>
 </blockquote>
 <p align="left"><font size="2" face="Verdana">After this we extract the data
 from the form and store it in the elements, exactly in the same way as we did
 in Section 2. In the end, write the following code:</font></p>
 <blockquote>
  <p align="left"><font color="#006600"><code>&lt;%<br>
  ' Check once again to see if the file loaded successfully. If it did&nbsp;<br>
  ' not, that means we are creating a new document and need to be sure
  to&nbsp;<br>
  ' insert the XML processing instruction.&nbsp;<br>
  If blnFileExists = False then&nbsp;<br>
  'Create the xml processing instruction.&nbsp;<br>
  Set objPI = objDom.createProcessingInstruction(&quot;xml&quot;,
  &quot;version='1.0'&quot;)&nbsp;<br>
  'Append the processing instruction to the XML document.&nbsp;<br>
  objDom.insertBefore objPI, objDom.childNodes(0)<br>
  End If<br>
  %&gt;</code></font></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">The <b>complete code </b>for
 appending the data is:&nbsp;&nbsp;</font></p>
 <blockquote>
  <p align="left"><code><font color="#006600" size="2">&lt;%<br>
  <br>
  '--------------------------------------------------------------------<br>
  'The "AppendFormData" Function accepts two parameters.<br>
  'strXMLFilePath - The physical path where the XML file will be saved.<br>
  'strFileName - The name of the XML file that will be saved.<br>
  '--------------------------------------------------------------------<br>
  <br>
  Function AppendFormData(strXMLFilePath, strFileName)<br>
  <br>
 'Declare local variables.<br>
 Dim objXML<br>
 Dim objRoot<br>
 Dim objField<br>
 Dim objFieldValue<br>
 Dim objattID<br>
 Dim objattTabOrder<br>
 Dim objPI<br>
 Dim x<br>
  <br>
  <br>
 'Instantiate the Microsoft XMLDOM.<br>
 Set objXML = server.CreateObject("Microsoft.XMLDOM")<br>
 objXML.preserveWhiteSpace = False<br>
  <br>
  <br>
  <br>
 blnFileExists = objXML.Load(strXMLFilePath &amp; "\" &amp; strFileName)<br>
  <br>
  <br>
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br>
  ' <b>            This part is changed </b>            '<br>
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br>
  <br>
  <br>
  'Test to see if the file loaded successfully.&nbsp;<br>
  If blnFileExists = True Then&nbsp;<br>
   'If the file loaded set the objRoot Object equal to the root element 'of the XML  document.&nbsp;<br>
    Set objRoot = objXML.documentElement&nbsp;<br>
  Else<br>
   'Create your root element and append it to the XML document.<br>
   Set objRoot = objXML.createElement("Resource_Repository")<br>
   objXML.appendChild objRoot&nbsp;<br>
  End If<br>
  <br>
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br>
  <br>
  'Create an element, "Resource".<br>
  Set objResource = objXML.createElement("Resource")<br>
  <br>
  ' Make &lt;Resource> a child of Root<br>
  objRoot.appendChild objResource<br>
  <br>
  'Create a new element, "text". This stores Description of the resource<br>
  Set objText = objXML.createElement("text")<br>
  <br>
  ' Assign the description to the newly created object<br>
  objText.Text = Request.Form("description")<br>
  <br>
  ' Make Text (description) a child of Resource<br>
  objResource.appendChild objText<br>
  <br>
  <br>
  ' create another child of &lt;Resource> ie &lt;link><br>
  Set objLink = objXML.createElement("Link")<br>
  <br>
  ' make &lt;link> a child of &lt;Resouce><br>
  objResource.appendChild objLink<br>
  <br>
  ' create one more element - &lt;url>&nbsp;<br>
  Set objUrl = objXML.createElement("url")<br>
  <br>
  ' capture URL value from the form and assign it to<br>
  ' the newly created &lt;url> element<br>
  objUrl.Text = Request.Form("url")<br>
  <br>
  ' make &lt;url> a child of &lt;link><br>
  objLink.appendChild objUrl<br>
  <br>
  ' create a &lt;linetext> element, assign it a value&nbsp;<br>
  ' and make it a child of &lt;link><br>
  Set objLineText = objXML.createElement("linetext")<br>
  objLineText.Text = Request.Form("title")<br>
  objLink.appendChild objLineText<br>
  <br>
  <br>
  ' and finally create an element &lt;Size> and make<br>
  ' it a child of &lt;Resource><br>
  Set objSize = objXML.createElement("Size")<br>
  objSize.Text = Request.Form("size")<br>
  objResource.appendChild objSize<br>
  <br>
  <br>
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br>
  ' <b>            This part is changed</b>            '<br>
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br>
  <br>
 'Check once again to see if the file loaded successfully. If it did<br>
 'not, that means we are creating a new document and need to be sure to<br>
 'insert the XML processing instruction.<br>
 If blnFileExists = False then<br>
  <br>
 'Create the xml processing instruction.<br>
 Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")<br>
  <br>
 'Append the processing instruction to the XML document.<br>
 objDom.insertBefore objPI, objDom.childNodes(0)<br>
 End If<br>
  <br>
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br>
  <br>
  'Save the XML document.<br>
  objXML.save strXMLFilePath &amp; "\" &amp; strFileName<br>
  <br>
  <br>
  'Release all of your object references.<br>
  Set objXML = Nothing<br>
  Set objRoot = Nothing<br>
  Set objResource = Nothing<br>
  Set objText = Nothing<br>
  Set objLink = Nothing<br>
  Set objUrl = Nothing<br>
  Set objLineText = Nothing<br>
  Set objSize = Nothing<br>
  End Function<br>
  <br>
  <br>
  'Do not break on an error.<br>
  On Error Resume Next<br>
  <br>
  <br>
  'Call the AppendFormData function, passing in the physical path to&nbsp;<br>
  'save the file to and the name that you wish to use for the file.<br>
  <b>AppendFormData &quot;C:\Inetpub\wwwroot\append\data&quot;,&quot;Resource.xml&quot;</b><br>
  <br>
  <br>
  'Test to see if an error occurred, if so, let the user know.<br>
  'Otherwise, tell the user that the operation was successful.<br>
  If err.number &lt;> 0 then<br>
  &nbsp;&nbsp; Response.write("Errors occurred while saving data.")<br>
  Else<br>
  &nbsp;&nbsp; Response.write("&lt;center>&lt;h2>Data Entry Successful&lt;/h2>&lt;P>The resource has been successfully added in the database.&lt;/center> ")<br>
  End If<br>
  %></font><font color="#006600" face="Verdana" size="2"><br>
  </font></code></p>
 </blockquote>
 <p align="left"><font face="Verdana" size="2">Hope you enjoyed this article.
 But this is still not over. XMLDOM contains rich set of functions and some of
 them are very amazing. I will continue to write on this series. Explore this
 whole new world of XML. <b>This is really exciting !&nbsp;<br>
 </b><br>
 </font><font face="Verdana" size="1">(I am in the Final Year of my B.E. in
 Computer Engineering and my Final Year project is development of a Complete
 Database Management System (DBMS) for storing and manipulating XML and a new
 query language XQL much like XQuery, which will be used to query XML data just
 as we today use SQL for RDBMS.)</font></p>
 <p align="left"><font face="Verdana" size="2">In the end, visit my two web
 sites and I hope you will like both of them:</font></p>
 <ul>
  <li>
   <p align="left"><font face="Verdana" size="2"><a href="http://www.vyomworld.com/index.asp"><b>http://www.vyomworld.com</b></a>&nbsp;
   : This contains a huge source code library, 100's of Placement Papers of
   companies world wide, GRE Antonym test of 1208 Questions, GRE Word List,
   Search Engine and Free Professional Certification.&nbsp;</font></li>
  <li>
   <p align="left"><font face="Verdana" size="2"><a href="http://amitmathur.8m.com"><b>http://amitmathur.8m.com</b></a>
   :&nbsp; This contains eBooks on Compiler Programming, Virus Programming,
   Online tests on C++, Java, Computer Fundamentals, Sybase, etc, TSR
   Articles, Discussion Forum, my resume, numerous articles written by me on
   hacking and IP Addressing and some contributions by the visitors and their
   comments.</font></li>
 </ul>
 <p align="left"><font face="Verdana" size="2"><b>Comments, Suggestions,
 Criticism ? </b>Mail me directly from the web page: <a href="http://amitmathur.8m.com/mailme.html">http://amitmathur.8m.com/mailme.html<br>
 <br>
 </a><b>If you have some really exciting ASP Scripts and want to host them on
 an ASP server free of cost without ADs, <a href="http://amitmathur.8m.com/mailme.html">contact
 me</a> and I will host them free for you on <a href="http://www.vyomworld.com/index.asp">my
 website</a>. Keep writing good and original scripts.<br>
 </b> <a href="http://amitmathur.8m.com/mailme.html"><br>
 </a>Thanks.</font></p>
 <p align="left"><b><font face="Verdana" size="2" color="#CC3300">Please vote
 and support this effort.</font></b></p>
 <hr size="1" color="#006600">
</blockquote>
Original Comments (3)
Recovered from Wayback Machine