A man from Argentina asked me for some sample code to add data from an HTML form into an XML file.

I have put together three files - they are shown below

Click here to see the input form in a new window

It needs you to be using IIS and to have MSXML4 installed on your server

First an XML file needs to exist - a sample of such an empty file is shown below - on my server it can be found at http://www.byershostinggeorge.co.uk/xmlstuff/dummy.xml

Next the HTML file is http://www.ibrus.com/testing/addtoxml.htm there is a link to it above.

It is a very skeletal file that should be fairly easy to understand, so I will not insult any readers intelligence by describing it !!



<input TYPE="TEXT" NAME="Field_1" size="48">Field 1 
(A mandatory field)<br>
<input TYPE="TEXT" NAME="Field_2" size="48">Field 2<br>
<input TYPE="TEXT" NAME="Field_3" size="48">Field 3<br>
<input TYPE="SUBMIT" VALUE="Save it">

<img src="http://www.sst-counters.co.uk/counter1.asp?c_id=C321121">


Next comes the ASP (Active Server Page) that does the work - it is written, of course, in Visual Basic Script.

To understand it, you will need to have a reasonable knowledge of VBScript. If you do not please feel free to enquire in the box at the bottom of this page (remember to include details of who you are and your EMail address if you want a reply.

 function createXMLDocFromFile(xmlFileName)
  set xmlDoc = Server.CreateObject("MSXML2.DomDocument.4.0")
  xmlDoc.async = False
     xmlDoc.Load Server.MapPath(xmlFileName)
  xmlDoc.setProperty "SelectionLanguage", "XPath"
  set createXMLDocFromFile = xmlDoc
 end function

 sub addNewNode()
  set newNode = trialXmlDoc.createNode(1, Request.Form("Field_1"), "")
  newNode.setAttribute "Field_2", Request.Form("Field_2")
  newNode.setAttribute "Field_3", Request.Form("Field_3")

 end sub


if Request.Form("Field_1") = "" then
	ReplyText = "Field 1 must contain something
	ReplyText = ReplyText & "<P>Press BACK on your browser"
	ReplyText = ReplyText & " and try again."
	Response.Write ReplyText
end if

'Get a unique file name

Application("AppTimeNo") = Application("AppTimeNo") + 1
SecDef = DateDiff("s",date,now)
If SecDef <> Application("SecDef") Then
	Application("AppTimeNo") = 1
End If
Application("SecDef") = SecDef
ReplyFile1 = (SecDef * 100) + Application("AppTimeNo")

sXMLOutFile = ReplyFile1 & ".XML"

 set trialXmlDoc = createXMLDocFromFile("dummy.xml")

 call addNewNode

 Response.contentType = "text/xml"
 Response.write trialXmlDoc.xml

 set trialXmlDoc = Nothing


I hope that you have found this interesting.
I hope that you have found this useful.
Either of those means that I did not waste my time.
Niether of these means that I am sorry that I wasted your time !!