ASP code permits a very simple web-based maintenance of our Quiz server. There is no database in this implementation. Instead a system of flat XML files is maintained .
<html> <head> <title>XML Upload Server</title> </head> <body>
Like PHP, an ASP file starts out in HTML mode, and an escape sequence such as <% marks the beginning of the interpretable script
<% ' receives data (xml from flash) ' 1: back-up currently active quiz ( old/quiz.xml ) ' 2: make newly editted quiz active ( quiz.xml ) ' 3: archive today's latest version (eg: old/20011125.xml ) ' 4: displays xml in a browser window Option Explicit Dim xmldom Dim XMLstr
Option Explicit forces all variables to be declared before use. (It would be nice to have this option in ActionScript and in PHP. We have seen how frustrating it can be when every typo can silently generate a phantom variable and an elusive bug.)
' xmldom = Server.CreateObject("MSXML2.DOMDocument.3.0") xmldom = Server.CreateObject("MSXML.DOMDocument")
We first create a powerful XML object in the call to the MSXML dynamic library. This code runs better with Version 3 of MSXML. It also works better when we do not hardcode the version numbers into the object request. In particular this makes it easier to move the code between two very different installations in development and the publishing environments.
We only need to ask the XML object to load itself from the data POSTed by the requesting client. Then we can create a string version of the object.
All this is much more sophisticated than PHP's simple event-driven parser. It is similar to the ActionScript implementation of the DOM-based functionality. (PHP ought to be there soon, however. At least two groups are now building new DOM-based XML modules for PHP.)
If IsEmpty( xmldom) Then Response.Write "XML load failure" ' Response.Flush If IsEmpty( Request.Form("xml")) Then Response.Status = 412 Response.End
Send Precondition Failed error code to the client. It asked you to update the XML file but neglected to send the new data.
Else Dim fs Dim filename Dim backname Dim thisfile Response.ContentType="text/xml" Response.Write Request.Form("xml")
Request is the data object that arrived from the Client. Response is the data that the server is sending back in return. These lines tell the client to get ready for XML data and then echo back the XML we just received.
filename = server.mappath( "quiz.xml" ) backname = server.mappath( "old/quiz..xml")
We need the operating system's names for the files that the web server knows as "quiz.xml" and "old/quiz.xml". (These files do not have to actually exist. In particular the "old/" one might not yet have been created.) Rather than a relative address in a space limited to the Internet domain, the operating system uses a disk identifier and directory path on the server machine to reach the physical file.
Set fs = CreateObject("Scripting.FileSystemObject")
This fs object is not a file handle. It is a file system handle. It is the object that performs all the file-oriented operations we need.
Set thisfile= fs.GetFile( filename ) thisfile.Copy( backname )
We can address a file without opening it and instruct it to make a copy of itself as the backup file. If there is a backup file already, it is overwritten.
Set thisfile= fs.OpenTextFile(filename,2,True) thisfile.write( Request.Form( "xml") ) thisfile.close
Now we open the file for writing and write into it the string representation of the XML object that was posted.
backname = server.mappath( "old/" & Year(Date()) & Month(Date()) & Day(Date()) & ".xml") Set thisfile= fs.GetFile( filename ) thisfile.Copy( backname )
To create a permanent backup that will not be overwritten by the next update, we make an archive copy of today's new XML using today's date to name it.
Set thisfile = Nothing Set fs = Nothing End if
Then we recover the memory.
Else Response.Write "Look I found it!" Response.Write XMLstr End if %> </body> </html>