Problem solve Get help with specific problems with your technologies, process and projects.

Convert HTML to XML

You can use HTML and ASP coding to convert your HTML form entries to XML.

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 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"?>
<address>1234 Some Dr.</address>
<city>Pleasant Hill</city>

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

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

<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">

 <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">


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


'Append the processing instruction

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

'Save the XML document. 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)
 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
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.

Dig Deeper on Microsoft Internet Information Services (IIS)

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.