HTML form data to XML conversion
Jasmit Kochhar

What do you do with data when it has to go to a system based on XML? This tip, excerpted from InformIT, shows how you can

    Requires Free Membership to View

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.

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. You have to register there, but the registration is free.

Did you like this tip? Let us know! E-mail to sound off.

Related Book

XSLT: Working with XML and HTML
Author : Khun Yee Fung
Publisher : Addison Wesley
Published : Dec 2000
Summary:
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.


This was first published in July 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.