-----------------------------------------------------------
New Message on MumbaiUserGroup
-----------------------------------------------------------
From: Yash
Message 1 in Discussion
Using XML to collect User Information. ========================== In a data
application, sometimes it's a good practice to divide a data collection into logical
parts. For instance, when you collect user information such as name, address, and
telephone number, you don't want that data to interfere with the collection of other
independent data. In this situation, you may have to divide the work across several
Web pages. This can be a difficult task considering that the connection between the
client and server is stateless. One way to tackle this obstacle is to collect the
data in parts and write to a database as you collect the data. However, if somewhere
along the line the user wants to cancel this process, you must clean the data out of
the database. This usually doesn't present a problem, but there are times when you
might want to write the data collection in one fell swoop. You could rewrite all of
the form elements from the previous page as hidden <INPUT> elements in the next page,
but this could become tedious to manage. That's where you can put your XML knowledge
to work. If you collect all your data in an XML document from one page to the next,
you can pass around the XML document's contents in one hidden <INPUT> element. Then,
when you need the document again, you can load the contents into a new XML document
for processing. Let's say that we'll collect four types of information from a user:
the user's name, address, telephone numbers (work and home), and job information. The
first page will collect the user's first name, middle initial, and last name. The
second page will collect the user's street address, city, state, and ZIP code. The
third page will collect the user's home and work phone numbers. The final page will
collect the user's employer name and the user's job title. The data will be collected
in an XML document on the server side and written as a hidden <INPUT> element on the
following Web page. The server-side scripting language you choose isn't important;
however, you'll want to ensure that it has functionality for parsing XML and provides
a DOM document with which to work. (I'd normally use ASP, but I've decided to use
Perl.) <html>
<head>
<title>page1</title>
</head>
<body>
<form method="POST" action="page2.pl">
First Name <input type="text" size="15" name="first_name"><br>
MI <input type="text" size="5" name="mi"><br>
Last Name <input type="text" size="15" name="last_name"><br>
<input type="submit" value="Continue"><br>
</form>
</body>
</html> I didn't put the hidden <INPUT> element on this page because the second page
is where I collect the form data and start storing it for future use.
#!/Perl/bin/perl use CGI qw/:standard/;
use XML::Simple; my $request = new CGI;
my $response = new CGI(""); print
$response->header(-type=>'text/html',-expires=>'-1d');
my $ref; my $sxml;
$sxml = '<root>' .
'<first_name/>' .
'<last_name/>' .
'<mi/>' .
'<address1/>' .
'<city/>' .
'<state/>' .
'<zip/>' .
'<home_phone/>' .
'<work_phone/>' .
'<employer/>' .
'<job_title/>' .
'</root>';
$ref = XMLin($sxml); # Gather all form elements and store to XML.
my $param;
for $param ($request->param) {
$ref->{$param} = $request->param($param) if ($ref->{$param});
} $sxml = XMLout($ref, noattr=>1, rootname=>'root'); print <<EOF;
<html>
<head>
<title>page2</title>
</head>
<body>
<form method="POST" action="page3.pl" name="thisForm">
Address <input type="text" size="15" name="address1"><br>
City <input type="text" size="15" name="city"><br>
State <input type="text" size="5" name="state"><br>
Zip <input type="text" size="15" name="zip"><br>
<input type="hidden" name="txtXML" value="$sxml">
<input type="submit" value="Continue"><br>
</form>
</body>
</html>
EOF exit; This Perl script uses the XML::Simple package as the XML parser. The
initial XML is loaded via XMLin(). The for. . . loop gathers all the form elements and
stores them into their respective XML nodes based on their node names (or in the case
of the XML::Simple parser, the key for the hash reference). Using the XMLout() method,
a string is created, which is then used to populate the value of the txtXML hidden
<INPUT> element. Then the HTML is delivered to the client. On the next page, the
value of the txtXML form element is used as the XML for the XML::Simple parser:
#!/Perl/bin/perl use CGI qw/:standard/;
use XML::Simple; my $request = new CGI;
my $response = new CGI(""); print
$response->header(-type=>'text/html',-expires=>'-1d');
my $ref; my $sxml;
$sxml = $request->param('txtXML');
$ref = XMLin($sxml); # Gather all form elements and store to XML.
my $param;
for $param ($request->param) {
$ref->{$param} = $request->param($param) if ($ref->{$param});
} $sxml = XMLout($ref, noattr=>1, rootname=>'root'); print <<EOF;
<html>
<head>
<title>page3</title>
</head>
<body>
<form method="POST" action="page4.pl" name="thisForm">
Home Phone <input type="text" size="15" name="home_phone"><br>
Work Phone <input type="text" size="15" name="work_phone"><br>
<input type="hidden" name="txtXML" value="$sxml">
<input type="submit" value="Continue"><br>
</form>
</body>
</html>
EOF exit; The form elements are gathered and the HTML is delivered with the resulting
XML stored to the txtXML hidden <INPUT> element. This method continues until we
collect all of the necessary data: #!/Perl/bin/perl use CGI qw/:standard/;
use XML::Simple; my $request = new CGI;
my $response = new CGI(""); print
$response->header(-type=>'text/html',-expires=>'-1d');
my $ref; my $sxml;
$sxml = $request->param('txtXML');
$ref = XMLin($sxml); # Gather all form elements and store to XML.
my $param;
for $param ($request->param) {
$ref->{$param} = $request->param($param) if ($ref->{$param});
} $sxml = XMLout($ref, noattr=>1, rootname=>'root'); print <<EOF;
<html>
<head>
<title>page4</title>
</head>
<body>
<form method="POST" action="page5.pl" name="thisForm">
Employer <input type="text" size="15" name="employer"><br>
Job Title <input type="text" size="15" name="job_title"><br>
<input type="hidden" name="txtXML" value="$sxml">
<input type="submit" value="Continue"><br>
</form>
</body>
</html>
EOF exit; Page 5 displays all of the content in the XML data: #!/Perl/bin/perl use
CGI qw/:standard/;
use XML::Simple; my $request = new CGI;
my $response = new CGI(""); print
$response->header(-type=>'text/html',-expires=>'-1d');
my $ref; my $sxml;
$sxml = $request->param('txtXML');
$ref = XMLin($sxml); # Gather all form elements and store to XML.
my $param;
for $param ($request->param) {
$ref->{$param} = $request->param($param) if ($ref->{$param});
} for $param (keys(%$ref)) {
print "<b>$param:</b> $ref->{$param}<br>\n";
} exit; The final for. . . loop takes the keys array from the XML hash reference and
uses it to print out the node names and values. You can use this XML data to insert
and update records in a database. Since the XML data is passed around like a cookie
(as a hidden <INPUT> element), it isn't necessary to maintain a server connection.
Also, you can easily manage the data from the client on the server side through the
power of XML. Using this technique, you could feasibly collect the XML data and
present it to a Web service for processing. REGARDS, YASH SHAH.
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]