Set the followingCodeSave it as asp_xml.asp and run it:
< %
' By Dicky 21:52:18 am QQ: 25941 E-mail: AppleBBS@GMail.Com
Const Issql = 1 ' Define the database type as SQL Server
Call Openconn (conn) ' Open Database Connection
Dim RS, SQL
Set RS = Server. Createobject ( " ADODB. recordset " )
SQL = " Select * from products order by productname"
Rs. Open SQL, Conn, 1 , 1 ' Query data records in read-only mode
If Rs. EOF Then
Response. Write " Sorry, no record! " ' If no record exists
Else
Dim Objxmldom, objrootnode, objnode
Set Objxmldom = Server. Createobject ( " Msxml2.domdocument " ) ' Create an XML Document Object
Set Objrootnode = Objxmldom. createelement ( " XML " ) ' Create Root Node
Objxmldom.doc umentelement = Objrootnode
Do While Not Rs. EOF ' Loop all records
' Response. Write RS ("productname") & "<br>"
Set Objrownode = Objxmldom. createelement ( " Row " ) ' Create parent node
Set Objnode = Objxmldom. createelement ( " Productname " ) ' Create a subnode
Objnode. Text = RS ( " Productname " )
Objrownode. appendchild (objnode)
Set Objnode = Objxmldom. createelement ( " Unitprice " )
Objnode. Text = RS ( " Unitprice " )
Objrownode. appendchild (objnode)
Set Objnode = Objxmldom. createelement ( " Unitsinstock " )
Objnode. Text = RS ( " Unitsinstock " )
Objrownode. appendchild (objnode)
Objrootnode. appendchild (objrownode)
Rs. movenext: Loop ' Loop ends
Objxmldom. Save " D: \ myxmldoc. xml " ' Writing XML files allows you to use variables to customize file names on pages.
Response. Write " <SCRIPT> alert ('Congratulations, the XML file is successfully written! '); </SCRIPT>"
Set Objnode = Nothing ' Destroy object
Set Objrownode = Nothing ' Destroy object
Set Objrootnode = Nothing ' Destroy object
End If
Rs. Close
Set RS = Nothing
Call Closeconn () ' Close database connection
Function Openconn (conn) ' Open Database Connection
Dim Connstr
If Issql = 1 Then ' For SQL Server databases
' SQL Server database connection parameters: User Name, user password, database name, connection name (local, IP address used in other places)
Dim Sqlusername, sqlpassword, sqldatabasename, sqllocalname
Sqlusername = " SA"
Sqlpassword = " "
Sqldatabasename = " Northwind"
Sqllocalname = " (Local )"
Connstr = " Provider = sqloledb; user id = " & Sqlusername & " ; Password = " & Sqlpassword & " ; Initial catalog = " & Sqldatabasename & " ; Data Source = " & Sqllocalname & " ;"
Else ' For access databases
Dim DB
' For the first use, modify the address of the local database and change the database name accordingly. For example, change Dicky. mdb to Dicky. asp to prevent malicious download of the Access database)
DB = " Dicky. mdb"
Connstr = " Provider = Microsoft. Jet. oledb.4.0; Data Source = " & Server. mappath (db)
End If
On Error Resume Next
Set Conn = Server. Createobject ( " ADODB. Connection " )
Conn. Open connstr
If Err Then
' Err. Clear
Set Conn = Nothing
Response. Write " Database Connection error. Check the connection string. "
Response. End
End If
Set RS = Server. Createobject ( " ADODB. recordset " )
End Function
Function Closeconn () ' Close database connection
Conn. Close
Set Conn = Nothing
End Function
% >