Page

18.3.3- Creating XML Documents from a Web Page

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 18. An Introduction to XML.

Not tagged.
<< 18.3.2- The W3C Document Object ModelChapter1818.3.4- Creating XML from a Relational Database >>

Creating XML Documents from a Web Page

Up to now, we have only seen how to create XML documents in a text editor. This requires the author of the files to understand the way in which we are marking up our data. However, there are going to be situations where we need to allow people to create XML files without understanding our markup, let alone understanding how to read our DTD.

 

ASP enables us to provide a mechanism, whereby we can allow users of a web page to create XML documents without the need to understand what format they are being stored in, let alone how to write them. In this section, we will look at a way in which we can allow people to visit a page on our site and create an XML file. One easy way to do this is by providing users with a form.

Creating XML Documents from a Form

So that users who do not understand how to mark up data can still create XML files, we provide them with a form. What they enter into the form will be written to an XML file on the server when they click the Submit button.

 

The screen shot opposite shows the form that we will be using in our example. Here we are allowing users to file a report using the simple form, the contents of the report will then be created in an XML file:

 

Chapter18_image016

As you can see, we have a drop down list box for the users to provide a department name, along with input boxes for a summary, a title and the body of the report. When the user clicks on Send Report, the contents of the form are POSTed to the server. When the server receives the information from the POST operation on the client, we need to construct an XML document from it. We could do this using a similar technique to that we used in the example of creating an XML document using the DOM and JavaScript earlier in the chapter. However, it would be easier to do some simple string manipulation. Once we have created the XML document we just need to write this to the server as a text-based file. We will do this using the FileSystemObject.

 

OK, let's see how all this works.

Try It Out – Building an XML Document from Form Data

1.    Let's start with the user form, which will be used to create our XML file. Open up your favorite text editor and enter the following code:

<HTML>

<HEAD>

<TITLE>Wrox Trading</TITLE>

</HEAD>

<BODY>

 

<FONT FACE="Arial" SIZE="6">Wrox Trading Daily Report Submission</FONT><HR>

 

<FORM ACTION="makeReport.asp" METHOD="post">

 

<P>Select Department: <SELECT id="Dept" name="Dept" style="HEIGHT: 22px; WIDTH: 131px">

<OPTION selected value="IT" name="Dept">IT</OPTION>

<OPTION value="Pharm" name="Pharm"> Pharmaceuticals</OPTION>

<OPTION value="Petro" name="Petro">Petrochemicals</OPTION>

</SELECT>

</P>

 

<P>Summary:<BR>

<INPUT id="KeyInfo" name="KeyInfo" style="Width:350px; Height:50px"></P>

 

<P>Title:<BR>

<INPUT id="Title" name="Title" style="Width:350px"></P>

 

<P>Report:<BR>

<INPUT id="Report" name="Report" style="Width: 500px; Height: 200px;"></P>

 

<P>Click here to submit the report:&nbsp;

<INPUT type="submit" value="Send Report">

</P>

 

</FORM>

</BODY>

</HTML>

2.    Save the file as report.htm in your XMLFiles directory. We now need to create an ASP page that will allow us to retrieve the form information, make the XML and save it to disk. Open a new file in your favorite editor and enter the following code:

<%@LANGUAGE="VBScript"%>

<HTML>

<BODY>

<FONT FACE="arial" SIZE="6">Creating Daily Report XML File</FONT><HR>

<P>

<%

'set variables collected from the form

'used to write to the XML file and populate session object for re-use

strDept = Request.Form("Dept")

strKeyInfo = Request.Form("KeyInfo")

strTitle = Request.Form("Title")

strReport = Request.Form("Report")

 

strDate = Year(Now) & "-" & Right("00" & Month(Now), 2) & "-" & _

Right("00" & Day(Now), 2)

 

'directory with full permissions set on Web server, where file will be created

strFileName = "c:\InetPub\wwwroot\BegASPFiles\XMLFiles\" & strDate & strDept & ".xml"

QUOT = Chr(34)

CRLF = VbCrLf

 

'create new file, overwriting any existing one

 

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.CreateTextFile(strFileName, True)

 

'write XML page headings to file

strLine = "<?xml version=" & QUOT & "1.0" & QUOT & " ?>"

objFile.WriteLine strLine & CRLF

 

strLine = "<dailyReport dateCreated=" & QUOT & strDate & QUOT & " dept=" & _

QUOT & strDept & Quot & ">"

objFile.WriteLine strLine & CRLF

 

strLine = " <keyInfo>" & strKeyInfo & "</keyInfo>"

objFile.WriteLine strLine & CRLF

 

strLine = " <title>" & strTitle & "</title>"

objFile.WriteLine strLine & CRLF

 

strLine = " <report>" & strReport & "</report>"

objFile.WriteLine strLine & CRLF

 

strLine = "</dailyReport>"

objFile.WriteLine strLine

 

objFile.Close

Set objjFile = Nothing

Set objFSO = Nothing

Response.Write("You have created the report for the " & strDept & _

" department, for the " & strDate & ".<BR>")

 

Response.Write("<HR>")

Response.Write (strReport)

Response.Write("<HR>")

 

%>

 

</BODY>

</HTML>

 

3.    Save the file as makeReport.asp in the XMLFiles folder.

4.    Open up report.htm and enter some values. Then click on the Submit button. You should end up with a screen that looks something like this, telling you the department and the report information:

Chapter18_image017

 

5.    Open up Windows Explorer and go to the folder that is holding your files. You should find that a new XML file has been created. Its name is constructed from the curent date and the name of the department you selected from the drop down list (for example, 1999-12-02IT.xml).Open up the XML file in a text editor. You should see something like this, with whatever you entered in the form between the tags:

<?xml version="1.0" ?>

 

<dailyReport dateCreated="1999-11-30" dept="IT">

 

<keyInfo>The summary information for the IT department goes here</keyInfo>

 

<title>The title of the report is held in the title</title>

 

<report>Finally the full report containing all the information is held in the Report section of the file</report>

 

</dailyReport>

How It Works

Let's start with our web page, report.htm. The details are added in HTML <FORM> elements, which will be sent to makeReport.asp on the server:

 

<FORM ACTION="makeReport.asp" METHOD="post">

 

We then have the drop down list box that allows the user to select for which department they are filing the report. The important bit of this is the value attribute, whose value will be sent to the server as the value of Dept, which is the name of the select list:

 

<SELECT id="Dept" name="Dept" style="HEIGHT: 22px;

WIDTH: 131px">

<OPTION selected value="IT" name="Dept">IT</OPTION>

<OPTION value="Pharm" name="Pharm">Pharmaceuticals</OPTION>

<OPTION value="Petro" name="Petro">Petrochemicals</OPTION>

</SELECT>

 

We then have three simple input boxes, whose id attributes will be used in the forms collection on the server to retrieve the important information.

 

<P>Summary:<BR>

<INPUT id="KeyInfo" type="text" name="KeyInfo" style="Width:350px; Height:50px"></P>

 

<P>Title:<BR>

<INPUT id="Title" type="text" name="Title" style="Width:350px"></P>

 

<P>Report:<BR>

<INPUT id="Report" type="text" name="Report" style="Width: 500px; Height: 200px;">

</P>

 

So, when the user clicks on the Send Report button, the form details are sent to makeReport.asp, which we shall look at next.

 

<P>Click here to submit the report:&nbsp;

<INPUT type="submit" value="Send Report">

</P>

</FORM>

Creating XML from the Form Data and Writing it to Disk

The file we used to do all the work was called makeReport.asp, so, let's see how this achieved its task.

 

We start with the familiar language statement, and write out some opening HTML:

 

<% LANGUAGE="VBScript"%>

<HTML>

<BODY>

<FONT FACE="arial" SIZE="6">Creating Daily Report XML File</FONT><HR>

<P>

 

The first real bit of work involves retrieving the data from the Forms collection of the ASP Request object. You will have seen this technique already in Chapter 7 . We set the values of the form data to variables that we can then use when it comes to creating the XML, and displaying the entry for the user to check.

 

<%

'set variables collected from the form

strDept = Request.Form("Dept")

strKeyInfo = Request.Form("KeyInfo")

strTitle = Request.Form("Title")

strReport = Request.Form("Report")

 

The variable strDept will hold the name of the department, strKeyInfo will hold the summary of the report, strTitle holds the title and strReport holds the full report.

 

We also need to use VBScript functions to create today's date in the format YYYY-MM-DD.

 

strDate = Year(Now) & "-" & Right("00" & Month(Now), 2) & "-" & _

Right("00" & Day(Now), 2)

 

Next we have to create the pathname and file for the XML document that we are going to create and store, in a variable called strFileName. We start by choosing the directory in which our XML files will be stored.

 

Our reports' filenames will be created from the date for the report, and a string representing the department:

 

  • IT for the IT department
  • Pharm for the pharmaceutical department
  • Petro for the petrochemical department

 

So, onto this path we add the date, using the strDate variable, and the department that the report is for (remember that we collected this from the forms collection and set it to a variable strDept). Of course, as this is an XML file we need to append the .xml file extension to the end of this string.

 

'directory with full permissions set on Web server, where file will be created

strFileName = "c:\InetPub\wwwroot\BegASPFiles\XMLFiles\" & strDate & strDept & _
".xml"

QUOT = Chr(34)

CRLF = VbCrLf

 

So, the IT department's report for the 21st January 2002 would be 2002-01-21IT.xml.

 

Note that the folder in which you are storing your reports must already exist, before you can run the page successfully.

 

Having done the preparatory work, we can create a text file on the server, in which we will store our report as XML. We do this using the FileSystemObject, as you will have seen in Chapter 10 . As you can see, this is where we need the file name and path that we just created in strFileName:

 

'create new file, overwriting any existing one

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.CreateTextFile(strFileName, True)

 

That has created the text file, so now we need to generate the XML to store in the file. The reports will be stored in the following format:

 

<?xml version="1.0" ?>

<dailyReport dateCreated="yyyy-mm-dd">

<keyInfo></keyInfo>

<title></title>

<report></report>

</dailyReport>

 

We can now start writing the XML to the file we have just created.

 

The creation of the file uses a string manipulation technique. We add the XML line-by-line, setting a variable to the line we want to create, and then writing it to the text file we have created. Let's look at the first line we are writing.

 

'write XML page headings to file

strLine = "<?xml version=" & QUOT & "1.0" & QUOT & " ?>"

objFile.WriteLine strLine & CRLF

 

As you can probably see, this will create the XML prolog. As with any ASP code, we have to watch for certain characters and here we add the quotation marks in a second string. This single line is then written to the file using the WriteLine method of the instance of the FileSystemObject that is held in the objFile.

Now we can start writing out the content of the XML file. Of course, this begins with the root node <dailyReport>, which has the date as an attribute.

 

strLine = "<dailyReport dateCreated=" & QUOT & strDate & QUOT & " dept=" _

& QUOT & strDept & Quot & ">"

objFile.WriteLine strLine & CRLF

 

Now we are ready to write the actual contents of the report to the file. Each of the element's contents is collected from the variable that we set earlier.

 

strLine = " <keyInfo>" & strKeyInfo & "</keyInfo>"

objFile.WriteLine strLine & CRLF

strLine = " <title>" & strTitle & "</title>"

objFile.WriteLine strLine & CRLF

strLine = " <report>" & strReport & "</report>"

objFile.WriteLine strLine & CRLF

 

Having written out the report, we add a closing root tag, and close the file that we have created.

 

strLine = "</dailyReport>"

objFile.WriteLine strLine

objFile.Close

Set objFile = Nothing

Set objFSO = Nothing

 

We also clean up our resources. The XML file will now be residing on the server in the directory that you specified.

 

We then let the user know that they have created the report, remind them of the date and department that it is for:

 

Response.Write ("You have created the report for the " & strDept & _

" department, for the " & strDate & ".<BR>")

 

Finally, we write out the report again, using the variable from the top of the page that collected the report from the form.

 

Response.Write (strReport)

Response.Write("<HR>")

%>

 

</BODY>

</HTML>

 

OK, we've covered how to create an XML document on the server using the DOM and how to create XML from a web page. Consider now that we already have a large amount of data stored in relational databases, so wouldn't it be helpful to expose this content as XML as well...?

<< 18.3.2- The W3C Document Object ModelChapter1818.3.4- Creating XML from a Relational Database >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |