First off I would like to say that I think that my question may be time consuming for people to solve it in a complete sense so I understand that it is totally possible that the complete answer is asking for just way too much, so anything to help me better understand, like: reading material, examples, links, and/or advice would be great and I do very much appreciate any and every comment I receive, good or bad it just makes me and this place alot better, finally, I would like to thank you all so much for everything that you do here, this is truly a place that was build by smart people, and people that care.
MY QUESTION
(using Classic ASP and SQL Server)
I know that it is possible to read a remote XML file and then insert it into a SQL Server database table. I found one example that does that it uses Classic ASP and MS Access but that can be changed to SQL Server with minimal coding the URL is: http://forums.aspfree.com/code-bank-54/classic-asp-import-remote-xml-file-into-database-152966.html
But the problem is that I cannot get it to work on my remote XML file, I tried to edit the classic asp code (found in the link above) for days and days and I just cannot get it to work the way I would like.
So I want to start from the beginning,
The XML file in question is located on: http://www.iftach.org/taxmatrix/charts/2Q2012.xml
I seen a couple of examples on how you can export the entire xml file into the SQL Server database (like do a BULK insert, see URL: http://support.microsoft.com/kb/316005) and also on how to extract some info. from the XML but my request is kind of odd since I want to check for the Country first and then get that Counties Rate only and not the other one, I want to do this for the entire xml file.
For example the xml file is something like this:
(or you can view the full xml file by clicking on the URL above)
<FILE>
<QUARTER>2Q2012</QUARTER>
<RECORD>
<JURISDICTION ID="#15">AB</JURISDICTION>
<COUNTRY>CAN</COUNTRY>
......
......
<RATE RATECHANGE="0" COUNTRY="US">0.3366</RATE>
<RATE RATECHANGE="0" COUNTRY="CAN">0.0900</RATE>
......
......
......
</RECORD>
<RECORD>
<JURISDICTION ID="#15">FL</JURISDICTION>
<COUNTRY>U.S.</COUNTRY>
......
......
<RATE RATECHANGE="0" COUNTRY="US">1.5700</RATE>
<RATE RATECHANGE="0" COUNTRY="CAN">1.3210</RATE>
......
......
......
</RECORD>
</FILE>
and so on….
Now I would like to insert that info into the SQL Server table called FFTR and name the column specific for each JURISDICTION
Like for example the above would be:
Field Name 1 --> "JURISDICTION_AB_CAN"
Field Name 2 --> "JURISDICTION_FL_US"
and so on...
NOTE:
The prefix JURISDICTION_ will always be the same only the two letters will change and the CAN can become US.
Another thing is if the COUNTRY is “CAN” then I would like to use the CAN Rate and if it’s U.S. I would like to use the US Rate and insert that info. into the database with the Field named “RATE”. (The Rate will always be 4 decimal places) the Rate I want is only under: <FUEL_TYPE>Special Diesel</FUEL_TYPE> I don’t need the other Rates.
And the last thing I would like to do is to have the <QUARTER>2Q2012</QUARTER> inserted into a Field named “Quarter”
So the final SQL Server database would look like this (using the 2 records as an example above)
Field Name: JURISDICTION_AB_CAN
Rate: 0.0900
Quarter: 2Q2012
Field Name: JURISDICTION_FL_US
Rate: 1.5700
Quarter: 2Q2012
So what I tried to do is this (see code below) and I got it to show each line but it doesn’t even come close to a solution:
<%
Option Explicit
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.setProperty "ServerHTTPRequest", True
xml.Load ("http://www.iftach.org/TaxMatrix/charts/2Q2012.xml")
Dim paragraph1,paragraph2,paragraph3,paragraph4,paragraph5,paragraph6
paragraph1 = xml.documentElement.childNodes(1).text
paragraph2 = xml.documentElement.childNodes(2).text
paragraph3 = xml.documentElement.childNodes(3).text
paragraph4 = xml.documentElement.childNodes(4).text
paragraph5 = xml.documentElement.childNodes(5).text
paragraph6 = xml.documentElement.childNodes(6).text
Set xml = Nothing
%>
<html>
<head>
<title></title>
</head>
<body>
<p align="center"><% = paragraph1 %></p>
<p align="center"><% = paragraph2 %></p>
<p align="center"><% = paragraph3 %></p>
<p align="center"><% = paragraph4 %></p>
<p align="center"><% = paragraph5 %></p>
<p align="center"><% = paragraph6 %></p>
</body>
</html>
I even think that adding it to a ADODB Recordset would be great and then I would insert it into SQL Server one by one or just loop it all in there, but it only shows me the columns I need the rows in there also. See code below:
<%
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = "Provider=MSDAOSP; Data Source=MSXML2.DSOControl.3.0;"
objRS.Open(Server.MapPath("2Q2012.xml"))
Response.Write(objRS.Fields(2) & "<br>") ' <-- Returns the Quarter only, that I need for the Quarter Field in the DB
'Response.Write(objRS.Fields(6) & "<br>") ' <-- Returns the entire xml page
Do While Not objRS.EOF
objRS.MoveNext
Loop
%>
<table border="1" width="100%">
<%
dim fld
Response.Write("<tr>")
For Each fld in objRS.Fields
If fld.Name <> "$Text" Then
Response.Write("<td>" & fld.Name & "</td>")
End If
Next
Response.Write("</tr>")
Do While Not objRS.EOF
Response.Write("<tr>")
For Each fld in objRS.Fields
If fld.Name <> "$Text" Then
Response.Write("<td>" & fld.Value & "</td>")
End If
Next
Response.Write("</tr>")
objRS.MoveNext
Loop
%>
</table>
Again, Thank you so much for any advice, links, or any help at all…
Finally!!! I made it work, if anyone needs the solution it is below:
Using the code I posted in the question I added the following code:
Note: I know that this is not the best way to do it since I’m not going after the TAGS/IDs/Names but the xml file will always stay formated the same so I just Looped, Cut out what I needed, and Inserted/Updated into the DB.
Note: By making small changes you can set it to Insert the data into SQL and/or update, should this be a problem for anyone please let me know I will be more then happy to help.
To all that have tried to solve my problem/question, Thank you so much for all of your hard work and effort.