
January 30th, 2007, 03:01 AM
|
 |
Contributing User
|
|
Join Date: Oct 2006
Location: Grantham, UK
Posts: 358
  
Time spent in forums: 4 Days 14 h 27 m 28 sec
Reputation Power: 6
|
|
classic ASP: import remote XML file into database
If any body would like it below is my full complete code to get XML data and store it into an access database.
Code:
<%
Dim XMLDom
Dim ItemID
Dim DbConn
Dim SQLString
Dim ANArticleNode
Dim CollectionOfArticleNodes
Set DbConn = Server.Createobject("ADODB.connection")
DbConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="
DbConn.ConnectionString = DbConn.ConnectionString + server.MapPath("databasename")
DbConn.Open
Set XMLDom = Server.CreateObject( "Msxml2.DOMDocument.3.0" )
XMLDom.async = false
XMLDom.Load ("feed address goes here")
Set CollectionOfArticleNodes = XMLDom.SelectNodes("InfoStreamResults/Article")
'-- Iterate the collection of Article Tags
For Each ANArticleNode in CollectionOfArticleNodes
ItemID = ANArticleNode.SelectSingleNode("@ID").text
Heading = ANArticleNode.SelectSingleNode("Heading").text
Contents = ANArticleNode.SelectSingleNode("Contents").text
sDate = ANArticleNode.SelectSingleNode("Date").text
'-- Delete the item from the local database if it exists
SQLString = "DELETE FROM NewsData WHERE DirectNewsID=" & ItemID & ";"
'response.Write(SQLString)
DbConn.Execute(SQLString)
'-- Insert the item into the local database
SQLString = "INSERT INTO NewsData ([DirectNewsID], [Heading] ,[Contents], [Date]) VALUES(" & ItemID &", '" & EncodeIt(Heading) & "', '"& EncodeIt(Contents) & "', #"& sDate & "#)"
'response.Write(SQLString)
DbConn.Execute(SQLString)
Next
Function EncodeIt(ByVal TextString)
TextString = Replace(TextString, "'", "''")
TextString = Replace(TextString, Chr(34), Chr(34)&Chr(34))
EncodeIt = TextString
End Function
%>
What the code does is first it deletes any entry in the database which is currently on the XML sheet and then it runs the insert to enter all of the data into the DB. It runs a delete first so that there are no double entries in the database.
Hope someone may find this helpful.
Last edited by Shadow Wizard : January 30th, 2007 at 03:30 AM.
|