title: How to write data from Form in HTML to XML with Javascript
body: <p>This is an assignment from my class. What I need to do is create a registration page. When the user presses the submit button, I have take all the information on the form and write it to an existing XML file using Javascript. This is done on the client side, only through HTML, Javascript, and XML. By the way, my Professor purposely did not teach us how to do this because he wants us to research on it by ourselves. Also, I'm not too familiar with Javascript, especially with the built in functions, if possible please explain what each line or method of code is doing.</p>
  
  <p>Let me begin, here's how my existing XML looks like:</p>
  
  <pre><code>    &lt;?xml version ="1.0" encoding="utf-8" ?&gt;
      &lt;!--GGFGFGFVBFVVVHVBV--&gt;
      &lt;PersonInfo&gt;
        &lt;Person Usrname="Bob111" Pswd="Smith111" personid="111" FirstName="Bob" LastName="Smith" DOB="01/01/1960" Gender="M" Title="Hello1"&gt; 
        &lt;/Person&gt;
      &lt;!-- several more lines of &lt;person&gt; here --&gt; 
      &lt;/PersonInfo&gt;  
  </code></pre>
  
  <p>When saving the form data, it has to follow all the layout within , basically I would need Usrname, Pswd, personid, and so on.</p>
  
  <p>Basically, from what I understand, I have to create the XML line "Person" from my registration page once I press submit. Then push it to the array that already have my XML information, then write over my XML document with the information on the array. My problem is, I have exactly no idea how to do that. </p>
  
  <p>For those who wants to know how my registration page looks like, here it is:</p>
  
  <pre><code>    &lt;html&gt;
  
      &lt;head&gt;
      &lt;title&gt;Registration&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="CSS_LABs.css" /&gt;
      &lt;/head&gt;
  
  
      &lt;body&gt;
  
      &lt;div class="form"&gt;
      &lt;form id="Registration" action="" method="get"&gt;
  
      Username:&lt;input type="text" name="usrname" maxlength="10"/&gt; &lt;br/&gt;
      Password:&lt;input type="password" name="pswd" maxlength="20"/&gt; &lt;br/&gt;
  
      &lt;hr&gt;
  
      PersonID:&lt;input type="text" name="PID" /&gt; &lt;br&gt;
  
      &lt;hr&gt;
  
      First Name:&lt;input type="text" name="fname"/&gt; &lt;br&gt;
      Last Name:&lt;input type="text" name="lname"/&gt;
  
      &lt;hr&gt;
  
      DOB:&lt;input type="text" name="dob" /&gt; &lt;br&gt;
  
      &lt;hr&gt;
  
      Gender:&lt;input type="text" name="sex" /&gt; &lt;br&gt;
  
      &lt;hr&gt;
  
      Title:&lt;input type="text" name="title" /&gt; &lt;br&gt;
  
      &lt;hr&gt;
  
      Secret Question:&lt;br&gt;
      &lt;select name="secret?"&gt;
      &lt;/select&gt; &lt;br&gt;
  
      Answer:&lt;input type="text" name="answer" /&gt; &lt;br&gt; &lt;br&gt;
  
      &lt;input type="submit" value="submit" /&gt;
      &lt;/form&gt;
      &lt;/div&gt;
  
      &lt;/body&gt;
  
      &lt;/html&gt;
  </code></pre>
  
  <p>By the way, I know certain information on my HTML document may not be worded properly, so do hope you guys don't mind. Also, I would also have to fix up my XML later by putting the answer to the secret question within  later. </p>
  
  <p>Alright, thanks a lot in advance guys.</p>
  
  <p><em><strong>UPDATE!!!</em></strong>  </p>
  
  <p>Here we go, I finally figured out how to create an XML document with Javascript, here's the code:  </p>
  
  <pre><code>    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var FILENAME = 'G:\\CST2309 - Web Programming 1\\Copy of Take Home Exam - Copy\\PersonXML2.xml';
  
      function SaveXML(UserData) 
      {   
          var file = fso.CreateTextFile(FILENAME, true);
          file.WriteLine('&lt;?xml version="1.0" encoding="utf-8"?&gt;\n');
          file.WriteLine('&lt;PersonInfo&gt;\n');
  
          for (countr = 0; countr &lt; UserData.length; countr++) {
              file.Write('    &lt;Person ');
              file.Write('Usrname="' + UserData[countr][0] + '" ');
              file.Write('Pswd="' + UserData[countr][1] + '" ');
              file.Write('PersonID="' + UserData[countr][2] + '" ');
              file.Write('FirstName="' + UserData[countr][3] + '" ');
              file.Write('LastName="' + UserData[countr][4] + '" ');
              file.Write('Gender="' + UserData[countr][5] + '" ');
              file.Write('DOB="' + UserData[countr][6] + '" ');
              file.Write('Title="' + UserData[countr][7] + '"');
              file.WriteLine('&gt;&lt;/Person&gt;\n');
          } // end for countr
  
          file.WriteLine('&lt;/PersonInfo&gt;\n');
          file.Close();
  
      } // end SaveXML function --------------------
  
      function LoadXML(xmlFile) 
      {
          xmlDoc.load(xmlFile);
          return xmlDoc.documentElement;
      } //end function LoadXML()
  
      function initialize_array() 
      {
          var person = new Array();
          var noFile = true;
          var xmlObj;
          if (fso.FileExists(FILENAME)) 
          {
              xmlObj = LoadXML(FILENAME);
              noFile = false;
              } // if
          else 
          {
              xmlObj = LoadXML("PersonXML.xml");
              //alert("local" + xmlObj);
              } // end if
  
          var usrCount = 0;
          while (usrCount &lt; xmlObj.childNodes.length) 
          {
              var tmpUsrs = new Array(xmlObj.childNodes(usrCount).getAttribute("Usrname"),
                                      xmlObj.childNodes(usrCount).getAttribute("Pswd"),
                          xmlObj.childNodes(usrCount).getAttribute("PersonID"),
                                      xmlObj.childNodes(usrCount).getAttribute("FirstName"),
                                      xmlObj.childNodes(usrCount).getAttribute("LastName"),
                                      xmlObj.childNodes(usrCount).getAttribute("Gender"),
                                      xmlObj.childNodes(usrCount).getAttribute("DOB"),
                                      xmlObj.childNodes(usrCount).getAttribute("Title"));
              person.push(tmpUsrs);
              usrCount++;
               }   //end while
          if (noFile == false)
              fso.DeleteFile(FILENAME);
          SaveXML(person);
      }   // end function initialize_array()  
  </code></pre>
  
  <p>What this code here is doing is that, it takes my original XML file and loads it up into an array so it can create a new XML file. Basically I got the creating the XML file part down, but still need help with the rest of my stuff.  </p>
  
  <p>My goal is trying to take my form data and push it into my existing array, not overwrite it, add to it, so I can update my existing XML file with the new registration information. This is where I have absolutely no idea how to do. Some pointers would be nice.</p>