Append XML File 

In this example we are appending data to an XML file, the details entered on the form and are stored in XML format on the server.

This can be used for any area where you want users to enter data but don't have a database to store the data, uses could include, Guest Book, Comments, Queries or any other area where you need user input.

Below the form you will see the last 5 entries using the function to add the comments to the XML File.

Append XML Code test


  This Is CAPTCHA Image
(enter the number below)

Comments


Posted By: Ian CoombsDate: 05/10/2009 09:33

Hi Paul

The code provided here has no spam protection as yet but it is something i need to add as you may have noticed spam appears here as well. You could add a CATCHPA to the form so it will check for genuine human user, or you could have email validation so when a user posts a comment it wont be displayed until they validate there email address.

Another solution is to only allow registered users to post comments but this is not usually an acceptable route as it prevents casual visitors from posting. although you can go with a dual system that works for registered users and casual visitors where registered users comments get added straight away and casual visitors have to validate there email.

With email validation i would save the comments to a separate file and then when they validate add it to the main file, or add a new label to the XML file and setting it to False, registered users are automatically set to true and casual users are false until the email validation is complete.

I also log the IP address of all posters and spammers IPs are sent to an on-line spammers database.

I hope this helps and if you need any help adding some spam protection send me an email.

Regards
Ian


Posted By: PaulBDate: 03/10/2009 09:29

Hi

I have tried this code and it works great except the spammers keep leaving comments. Is there anyway to prevent spam using your code.

Thanks
Paul


Posted By: nutbush101Date: 25/07/2009 14:26

Great code and it's nice to see working examples rather than just a block of code that don't work.

Keep up the good work.


Posted By: James burkeDate: 25/07/2009 10:20

I like the way this code works, how can i add it to my site?

I am new to ASP and have no knowledge of XML could you help me?

Regards
James


Posted By: Mark SDate: 23/07/2009 08:14

This works well, i am going to use it on my site.

Thanks for the code.


Code: processAdd.asp
<%
Function addNewCommenttoXML(strXMLFilePath, strFileName)

	'Declare local variables.
	Dim objDom
	Dim objRoot
	Dim objRecord
	Dim objField
	Dim objFieldValue
	Dim objattID
	Dim objattTabOrder
	Dim objPI
	Dim blnFileExists
	Dim x
	
	
	'Instantiate the Microsoft XMLDOM.
	Set objDom = server.CreateObject("Microsoft.XMLDOM")
	objDom.preserveWhiteSpace = True
	
	
	'Call the Load Method of the XMLDOM Object. The Load Method has a
	'boolean return value indicating whether or not the file could be
	'loaded. If the file exists and loads it will return true, otherwise,
	'it will return false.
	blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)
	
	
	'Test to see if the file loaded successfully.
	If blnFileExists = True Then
		'If the file loaded set the objRoot Object equal to the root element
		'of the XML document.
		Set objRoot = objDom.documentElement
	Else
		'Create your root element and append it to the XML document.
		Set objRoot = objDom.createElement("guestbook")
		objDom.appendChild objRoot
	End If
	
	'Create the new container element for the new record.
	Set objRecord = objDom.createElement("entry")
	objRoot.appendChild objRecord
	
	'Iterate through the Form Collection of the Request Object.
	For x = 1 To Request.Form.Count
	
		'Check to see if "btn" is in the name of the form element. If it is,
		'then it is a button and we do not want to add it to the XML
		'document".
		If instr(1,Request.Form.Key(x),"btn") = 0 Then
			
			'Create an element with the name of the form item.
			Set objField = objDom.createElement(Request.Form.Key(x))
			
			'Set the value of the element equal to the value of the
			'current field in the Form Collection.
			objField.Text = Replace(Request.Form(x), chr(13), "<br />")
			
			'Append the field element as a child of the new record container
			'element, contact.
			objRecord.appendChild objField
			
		End If
	Next 
	
	'Check once again to see if the file loaded successfully. If it did
	'not, that means we are creating a new document and need to be sure to
	'insert the XML processing instruction.
	If blnFileExists = False then
		'Create the xml processing instruction.
		Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
		
		'Append the processing instruction to the XML document.
		objDom.insertBefore objPI, objDom.childNodes(0)
	End If
	
	'Save the XML document.
	objDom.save strXMLFilePath & "\" & strFileName
	
	'Release all of your object references.
	Set objDom = Nothing
	Set objRoot = Nothing
	Set objRecord = Nothing
	Set objField = Nothing
	Set objFieldValue = Nothing
	Set objattID = Nothing
	Set objattTabOrder = Nothing
	Set objPI = Nothing

End Function
%>

The "AddNewCommenttoXML" Function accepts two parameters.
strXMLFilePath - The physical path where the XML file will be saved.
strFileName - The name of the XML file that will be saved.

You call the function with:
 addNewCommenttoXML "c:\inetpub\website\folder", "XMLFileName.xml" 
and you will need to have write permission on the final folder for the IUSER_ServerName user on the web server.

If the file dose not exist then the first time the script is run it will create the file and any subsequent use of the script will append to the file.