--------------------------------------------------------------------
SearchWin2000.com's Developer Tip
--------------------------------------------------------------------

TODAY'S DEVELOPER TIP: HTML form data to XML conversion

============================================================
Sponsored by VeriSign - The Internet Trust Company 
============================================================
Upgrade your server security to 128-bit SSL encryption! Get
VeriSign's FREE guide, "Securing Your Web Site for Business." You
will learn everything you need to know about using 128-bit SSL to
encrypt your e-commerce transactions for serious online security.
Click here!
http://www.verisign.com/cgi-bin/go.cgi?a=n046640330014000

====================================================================
"HTML form data to XML conversion"

By Jasmit Kochhar

What do you do with data when it has to go to a system based on XML?
This tip, excerpted from InformIT at
http://www.informit.com/content/index.asp?product_id={916AE4E6-B887-429D-94DB-7C9C039E1F48},
 
shows how you can easily convert from HTML to XML data. Do you have a
tip from your experience? You can easily submit one and enter our tip
contest by following this link
http://searchwin2000.techtarget.com/tipsSubmit/1,289485,sid1,00.html.
_____________________________________________________________________

Our objective in this article is to suggest some simple methods to
use HTML forms to construct XML documents, which can then be passed
to a database or saved as a file. We will start with a simple XML
document.

A Simple XML Document

In this case, constructing the XML document requires iterating
through the form data in the request object and writing the value of
each form field as an XML Document Node.

To exclude the form fields that we do not wish to include within the
XML document, we will append the string "__exclude__" to the field
name in the HTML form data.

Assume that we have a simple XML document that we need to construct,
as follows:

<?xml version="1.0"?>
<function>
<func_name>ADD_USER</func_name>
<firstname>Jasmit</firstname>
<middlename>Singh</middlename>
<lastname>Kochhar</lastname>
<address>1234 Some Dr.</address>
<city>Pleasant Hill</city>
<state>CA</state>
<zip>94523</zip>
</function>

The data is captured using the HTML form given as follows
(NewUser.html):

<html>
<head><title>New User
Information</title></head>
<body>

<form action="ProcessData.asp" method="post"
enctype="application/x-www-form-urlencoded">

<h2>Please provide the following information</h2>

<input type="hidden" name="func_name" value="ADD_USER">
 <table>

 <tr><td><b>First Name:</b></td>
   <td><input type="text" name="firstname"
size="40"></td></tr>

 <tr><td><b>Middle Name:</b></td>
   <td><input type="text" name="middlename"
size="40"></td></tr>
 
 <tr><td><b>Last Name:</b></td>
   <td><input type="text" name="lastname"
size="40"></td></tr>

 <tr><td><b>Street Address:</b></td>
   <td><input type="text" name="address"
size="40"></td></tr>

 <tr><td><b>City, State - Zip:</b></td>
   <td><input type="text" name="city" size="30">,
     <input type="text" name="state" size="2"> 
<input type="text" name="zip" size="10"></td></tr>

<tr><td colspan="2">
<input type="submit" name="__exclude__Submit" value="Submit">
</td></tr>

</table>
</form>
</body></html>

The only difference in the preceding HTML form is the form field for
the button with the name "__exclude__Submit." We wish to include all
other input variables as part of the XML document generated.

The form is submitted from a user's Web browser to the Web server
that sends the data to the ASP page ProcessData.asp. The details for
the page are as follows:

<%

Option Explicit

'-------------------- ProcessData.asp-------------------------------

'--------------------------------------------------------------------
' The Function saveXMLData saves the XML doc in the filename
specified.
' The function can be easily adapted to send the data directly to
' a database object
'--------------------------------------------------------------------

Function saveXMLData(strPath, strFileName)

 'Declare local variables.
 Dim aXMLDoc
 Dim aRootNode
 Dim aFormVar
 Dim aPI
 Dim Item

'Create an XMLDOM Object
 Set aXMLDoc = server.CreateObject("Microsoft.XMLDOM")
 aXMLDoc.preserveWhiteSpace = True


'Create the root node for the document
 Set aRootNode = aXMLDoc.createElement("function")
 aXMLDoc.appendChild aRootNode


'Iterate the Request Object for all form data
 For Each item in Request.Form

'Do not include the variable if it contains __exclude__
   If instr(1,item,"__exclude__") = 0 Then
     Set aFormVar =aXMLDoc.createElement(item)
     aFormVar.Text = Request.Form(item)
     aRootNode.appendChild aFormVar
   End If

 Next


'Append the processing instruction

 Set aPI = aXMLDoc.createProcessingInstruction("xml","version='1.0'")
 aXMLDoc.insertBefore aPI, aXMLDoc.childNodes(0)


'Save the XML document.
 aXMLDoc.save strPath & "\" & strFileName


'Release all references.
 Set aXMLDoc = Nothing
 Set aRootNode = Nothing
 Set aFormVar= Nothing
 Set item = Nothing
 Set aPI = Nothing
End Function

' The page starts here
On Error Resume Next

' Save the XML Data
SaveXMLData "c:","NewUser.xml"


If err.number <> 0 then
 Response.write("Errors occurred while saving your information."+
Err.Description)
Else
 Response.write("Thank you for submitting your information.")
End If
%>
_____________________________________________________________________
To read this entire tip, click over to InformIT at
http://www.informit.com/content/index.asp?product_id={916AE4E6-B887-429D-94DB-7C9C039E1F48}.
 
You have to register there, but the registration is free.

====================================================================
DID YOU LIKE THIS TIP? 
====================================================================
Whether you loved it or hated it, why not let us know? Also, let us
know what Windows development issues you want to read more about.
Email your comments to mailto:tips@searchwin2000. 

========================================================
Featured Book
========================================================
XSLT: Working with XML and HTML 
Author: Khun Yee Fung 
Publisher: Addison Wesley 
Published: Dec 2000 
 
The book includes a detailed reference to XSLT and XPath elements and
functions, as well as a CD-ROM containing all code, plus exclusive
software enabling the reader to explore XSLT's most challenging
feature, expressions.MARKET:For all Web developers, site
administrators, and others working with XML, XSL, or XSLT. 

http://www.digitalguru.com/dgstore/product.asp?isbn=0201711036&ac_id=73

====================================================================
WIN! WIN! WIN!
--------------------------------------------------------------------
Our July Tip of the Month contest just got started! Get in on the
action for your chance to win this month's grand prize -- a Garmin
eMap handheld GPS!  

Submit your tip today at
http://searchwin2000.techtarget.com/tipsHallOfFame/0,289489,sid1_prz751595_cts751583,00.html
 
====================================================================
If you would like to sponsor this or any TechTarget newsletter,
please contact Mike Kelly at [EMAIL PROTECTED]
====================================================================







If you no longer wish to receive this newsletter simply reply to 
this message with "REMOVE" in the subject line.  Or, visit 
http://searchWin2000.techtarget.com/register 
and adjust your subscriptions accordingly. 

If you choose to unsubscribe using our automated processing, you 
must send the "REMOVE" request from the email account to which 
this newsletter was delivered.  Please allow 24 hours for your 
"REMOVE" request to be processed.

Reply via email to