December 12th, 2007

How to create XML from an Access (mdb) database with ASP

OK, so I recently created this code with VBS, but it would work with ASP as well. Basically, you need to place the script in a folder where it can get write permissions to create the XML file. Then you'll connect to the Access database like you would normally. Although to be perfectly honest, you could use this script to create an XML from data in just about any type of database that you can connect to!


'=== declare variables
Dim objConn, strConnect, strSQL, rs, tb, mdbFile, objFSO, xmlFile, objWrite

'=== filename variables
xmlFile = Server.MapPath(""inventory.xml")
mdbFile	= Server.MapPath("database.mdb")

'=== tab character for xml file
tb = chr(9)

'=== instantiate objects
set objFSO  = Server.CreateObject( "Scripting.FileSystemObject" )
Set objConn = Server.CreateObject( "ADODB.Connection" )

'=== connect to database
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbFile

'=== open/create xml file
If Not objFSO.FileExists( xmlFile ) Then objFSO.CreateTextFile( xmlFile )
set objWrite = objFSO.OpenTextFile( xmlFile, 2 )

'=== open the xml file
objWrite.WriteLine("<?xml version=""1.0"" encoding=""ISO-8859-1""?>")
objWrite.WriteLine("<data>")

strSQL = "SELECT * FROM table WHERE 1=1"
Set rs = objConn.Execute(StrSQL)

'=== loop through results
Do While not rs.EOF
    objWrite.WriteLine(tb & "<item>")
    objWrite.WriteLine(tb & tb & "<id>" & rs("id") & "</id>")
    objWrite.WriteLine(tb & tb & "<product>" & replace(rs("product"),"&","&amp;") & "</product>")
    objWrite.WriteLine(tb & tb & "<color>" & rs("color") & "</color>")
    objWrite.WriteLine(tb & tb & "<size>" & rs("size") & "</size>")
    objWrite.WriteLine(tb & "</item>")
    rs.MoveNext
Loop

'=== finish xml file
objWrite.WriteLine("</data>")
objWrite.Close()
  • Share/Bookmark

7 Responses to ' How to create XML from an Access (mdb) database with ASP '

Subscribe to comments with RSS or TrackBack to ' How to create XML from an Access (mdb) database with ASP '.

  1. Jay N prasad said,

    on March 26th, 2008 at 3:01 am

    Hi,
    I was looking for a solution " How to create XML from an Access (mdb) database with ASP"

    And i got the solution from your website .You saved my time.

    Thanks you very much

    jay N prasad


  2. on June 20th, 2008 at 9:24 am

    Cheapest tramadol….

    Tramadol….

  3. Sarwar Zahan said,

    on November 3rd, 2008 at 11:51 am

    Hi,
    The solution is very helpful and end my search.


  4. on February 24th, 2009 at 4:39 am

    Thank you for the solution. Never thought it was soo easy.

    Lunderstedt consulting for Quality and affordable Email and consumer marketing lists

    Thank you sooooo much :-)

  5. Joey said,

    on July 19th, 2009 at 7:58 pm

    I wish you had a downlode of your code… I will get it to work thanks man.

  6. Joey said,

    on July 19th, 2009 at 8:02 pm

    xmlFile = Server.MapPath(""inventory.xml")

    is wrong it should be
    xmlFile = Server.MapPath("inventory.xml")

  7. Joey said,

    on July 20th, 2009 at 8:43 pm

    You should show how to open and display the xml file Thanks Joey B.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word