Andrew Angell
asked on
How to edit this script involving XML parsing...???
I've got a script setup right now that takes an XML and grabs a specific node based on the value of one of the elements and then parses out the data for that node. An XML example is here: http://www.carvertubs.com/temp/applications/freightQuote/xmlResponse/15497231.xml. The current script I'm using is as follows:
'Select the <CARRIER> node based on the value of <SCAC> child node
Set centralFreightNode = objXML.selectSingleNode("/ FQQUOTE/CA RRIER[SCAC ='CEDE']")
'Make sure the node was found
If NOT TypeName(centralFreightNod e)="Nothin g" Then
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS ingleNode( "RATE").te xt
freightCost = centralFreightNode.selectS ingleNode( "DETAIL/FR EIGHTCOST" ).text
arrivalNotification = centralFreightNode.selectS ingleNode( "DETAIL/AR RIVAL_NOTI FICATION") .text
residentialFee = centralFreightNode.selectS ingleNode( "DETAIL/NO N-COMMERCI AL_DELIVER Y").text
liftGate = centralFreightNode.selectS ingleNode( "DETAIL/LI FTGATE_DEL IVERY").te xt
fuelSurcharge = centralFreightNode.selectS ingleNode( "DETAIL/FU EL_SURCHAR GE").text
transitTime = centralFreightNode.selectS ingleNode( "TRANSIT") .text
Set centralFreightNode = Nothing
Else
'Handle if not found
End If
The reason behind this is that we only wanted to ship with Central Freight, which isn't always returned by the API, depending on the particular request. So, if Central Freight is returned by the API this grabs that data and displays, otherwise I simply display a message asking them to call.
We've added a couple of carriers to the list of people that we can use. These carriers have different values, of course, for FQQUOTE/CARRIER/SCAC element. There are 3 different ones I can use now, CEDE, RDWY, and DAFG. How can I edit this so that it does what it's doing now for all 3 of these carriers, and then only returns the data for the one with the lowest value for RATE? At times all 3 will come back, at times only 1 or 2 will, and at very rare times none could come back.
Any information on this would be greatly appreciated. Thanks!
'Select the <CARRIER> node based on the value of <SCAC> child node
Set centralFreightNode = objXML.selectSingleNode("/
'Make sure the node was found
If NOT TypeName(centralFreightNod
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS
freightCost = centralFreightNode.selectS
arrivalNotification = centralFreightNode.selectS
residentialFee = centralFreightNode.selectS
liftGate = centralFreightNode.selectS
fuelSurcharge = centralFreightNode.selectS
transitTime = centralFreightNode.selectS
Set centralFreightNode = Nothing
Else
'Handle if not found
End If
The reason behind this is that we only wanted to ship with Central Freight, which isn't always returned by the API, depending on the particular request. So, if Central Freight is returned by the API this grabs that data and displays, otherwise I simply display a message asking them to call.
We've added a couple of carriers to the list of people that we can use. These carriers have different values, of course, for FQQUOTE/CARRIER/SCAC element. There are 3 different ones I can use now, CEDE, RDWY, and DAFG. How can I edit this so that it does what it's doing now for all 3 of these carriers, and then only returns the data for the one with the lowest value for RATE? At times all 3 will come back, at times only 1 or 2 will, and at very rare times none could come back.
Any information on this would be greatly appreciated. Thanks!
ASKER
ok, this looks good, but I'm not getting the actual stuff to display when the carriers are returned. I'm just getting the 'handle if not found part no matter what gets returned in the XML. Here's a copy of the XML that I just got back that includes CEDE and RDWY but I got the error back as if none of them were returned: http://www.carvertubs.com/temp/applications/freightQuote/xmlResponse/15592895.xml
Here is the complete section of my script:
'grab the quotes for each shipper specefied and store the lowest rate in variables
dim minrateval
dim minrateshipper
dim shipper
dim shippers(3)
shippers(0) = "CEDE"
shippers(1) = "RDWY"
shippers(2) = "DAFG"
minrateval = 9999999999999999 'a very large number
for each shipper in shippers
Set centralFreightNode = objXML.selectSingleNode("/ FQQUOTE/CA RRIER[SCAC ='"&shippe r&"']")
If NOT TypeName(centralFreightNod e)="Nothin g" Then
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS ingleNode( "RATE").te xt
Set centralFreightNode = Nothing
if carrierrate < minrateval then
minrateval = carrierrate
minrateshipper = shipper
end if
end if
next
'Select the <CARRIER> node based on the value of <SCAC> child node
Set centralFreightNode = objXML.selectSingleNode("/ FQQUOTE/CA RRIER[SCAC ='"&minrat eshipper&" ']")
'Make sure the node was found
If NOT TypeName(centralFreightNod e)="Nothin g" Then
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS ingleNode( "RATE").te xt
handlingFee = Round((carrierRate * .1),2)
handlingFee = FormatNumber((handlingFee) ,2)
rateWithHandling = cdbl(carrierRate) + cdbl(handlingFee)
rateWithHandling = FormatNumber((rateWithHand ling),2)
freightCost = centralFreightNode.selectS ingleNode( "DETAIL/FR EIGHTCOST" ).text
If destResidence = "TRUE" Then
arrivalNotification = centralFreightNode.selectS ingleNode( "DETAIL/AR RIVAL_NOTI FICATION") .text
residentialFee = centralFreightNode.selectS ingleNode( "DETAIL/NO N-COMMERCI AL_DELIVER Y").text
End If
If destLiftGate = "TRUE" Then
liftGate = centralFreightNode.selectS ingleNode( "DETAIL/LI FTGATE_DEL IVERY").te xt
End If
fuelSurcharge = centralFreightNode.selectS ingleNode( "DETAIL/FU EL_SURCHAR GE").text
transitTime = centralFreightNode.selectS ingleNode( "TRANSIT") .text
If Request.QueryString("tubSh ell") = 1 Then
totalPrice = FormatNumber(cdbl(productP rice) + cdbl(rateWithHandling),2)
Else
totalPrice = FormatNumber(cdbl(eBayPric e) + cdbl(rateWithHandling),2)
End If
Set centralFreightNode = Nothing
%>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td width="149" align="right"><strong>Ship ping Cost: </strong></td>
<td width="237">$<%=rateWithHa ndling%></ td>
</tr>
<tr>
<td colspan="2" align="center"><span class="copyright"><strong> NOTE</stro ng>: Changing fuel prices can cause slight differences in shipping rates on any given day. The rate returned here is a REAL-TIME quote as of <%=date()%>.</span></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
</table>
<%
Else 'Node was not found, handle this here
%>
We're Sorry. None of our carriers returned a rate for this particular quote. Please give us a call TOLL FREE at 866-592-8169 to receive a shipping quote.
<%
End If
%>
Any ideas?
Here is the complete section of my script:
'grab the quotes for each shipper specefied and store the lowest rate in variables
dim minrateval
dim minrateshipper
dim shipper
dim shippers(3)
shippers(0) = "CEDE"
shippers(1) = "RDWY"
shippers(2) = "DAFG"
minrateval = 9999999999999999 'a very large number
for each shipper in shippers
Set centralFreightNode = objXML.selectSingleNode("/
If NOT TypeName(centralFreightNod
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS
Set centralFreightNode = Nothing
if carrierrate < minrateval then
minrateval = carrierrate
minrateshipper = shipper
end if
end if
next
'Select the <CARRIER> node based on the value of <SCAC> child node
Set centralFreightNode = objXML.selectSingleNode("/
'Make sure the node was found
If NOT TypeName(centralFreightNod
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS
handlingFee = Round((carrierRate * .1),2)
handlingFee = FormatNumber((handlingFee)
rateWithHandling = cdbl(carrierRate) + cdbl(handlingFee)
rateWithHandling = FormatNumber((rateWithHand
freightCost = centralFreightNode.selectS
If destResidence = "TRUE" Then
arrivalNotification = centralFreightNode.selectS
residentialFee = centralFreightNode.selectS
End If
If destLiftGate = "TRUE" Then
liftGate = centralFreightNode.selectS
End If
fuelSurcharge = centralFreightNode.selectS
transitTime = centralFreightNode.selectS
If Request.QueryString("tubSh
totalPrice = FormatNumber(cdbl(productP
Else
totalPrice = FormatNumber(cdbl(eBayPric
End If
Set centralFreightNode = Nothing
%>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td width="149" align="right"><strong>Ship
<td width="237">$<%=rateWithHa
</tr>
<tr>
<td colspan="2" align="center"><span class="copyright"><strong>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
</table>
<%
Else 'Node was not found, handle this here
%>
We're Sorry. None of our carriers returned a rate for this particular quote. Please give us a call TOLL FREE at 866-592-8169 to receive a shipping quote.
<%
End If
%>
Any ideas?
I'm sorry, I have very restricted web access here at work. Can you cut and paste the xml?
Ahh, also:
dim shippers(3) -> dim shippers(2)
I always forget in vb that the number in the brackets is the upper bound, not the number of elements in the array...
its doing an extra loop where shipper = "" and prolly returning zero for a nonexistant nodes value of carrierRate, making it the lowest rate and therefore the default carrier.
dim shippers(3) -> dim shippers(2)
I always forget in vb that the number in the brackets is the upper bound, not the number of elements in the array...
its doing an extra loop where shipper = "" and prolly returning zero for a nonexistant nodes value of carrierRate, making it the lowest rate and therefore the default carrier.
ASKER
Here's the XML...I switched the shippers(3) to shippers(2) and I'm getting the same result.
<?xml version="1.0" encoding="ISO-8859-1"?><FQ QUOTE QUOTEID="15676862"><CARRIE R OPTIONID="1"><CARRIERNAME> DO NOT USE</CARRIERNAME><SCAC>DNU E</SCAC><R ATE>$70.80 </RATE><DE TAIL><FREI GHTCOST>$6 0.00</FREI GHTCOST><F UEL_SURCHA RGE>$10.80 </FUEL_SUR CHARGE></D ETAIL><TRA NSIT>2</TR ANSIT></CA RRIER><CAR RIER OPTIONID="2"><CARRIERNAME> MID-STATES EXPRESS</CARRIERNAME><SCAC >MSXN</SCA C><RATE>$8 7.44</RATE ><DETAIL>< FREIGHTCOS T>$69.70</ FREIGHTCOS T><FUEL_SU RCHARGE>$1 7.74</FUEL _SURCHARGE ></DETAIL> <TRANSIT>2 </TRANSIT> </CARRIER> <CARRIER OPTIONID="3"><CARRIERNAME> USF HOLLAND</CARRIERNAME><SCAC >HMES</SCA C><RATE>$8 8.99</RATE ><DETAIL>< FREIGHTCOS T>$70.56</ FREIGHTCOS T><FUEL_SU RCHARGE>$1 8.43</FUEL _SURCHARGE ></DETAIL> <TRANSIT>1 </TRANSIT> </CARRIER> <CARRIER OPTIONID="4"><CARRIERNAME> LAKEVILLE EXPRESS</CARRIERNAME><SCAC >LAKE</SCA C><RATE>$8 9.94</RATE ><DETAIL>< FREIGHTCOS T>$77.40</ FREIGHTCOS T><FUEL_SU RCHARGE>$1 2.54</FUEL _SURCHARGE ></DETAIL> <TRANSIT>1 </TRANSIT> </CARRIER> <CARRIER OPTIONID="5"><CARRIERNAME> DAYTON FREIGHT LINES</CARRIERNAME><SCAC>D AFG</SCAC> <RATE>$94. 76</RATE>< DETAIL><FR EIGHTCOST> $78.29</FR EIGHTCOST> <FUEL_SURC HARGE>$16. 47</FUEL_S URCHARGE>< /DETAIL><T RANSIT>1</ TRANSIT></ CARRIER><C ARRIER OPTIONID="6"><CARRIERNAME> SAIA MOTOR FREIGHT</CARRIERNAME><SCAC >SAIA</SCA C><RATE>$1 02.99</RAT E><DETAIL> <FREIGHTCO ST>$84.04< /FREIGHTCO ST><FUEL_S URCHARGE>$ 18.95</FUE L_SURCHARG E></DETAIL ><TRANSIT> 1</TRANSIT ></CARRIER ><CARRIER OPTIONID="7"><CARRIERNAME> OLD DOMINION FREIGHT LINE</CARRIERNAME><SCAC>OD FL</SCAC>< RATE>$112. 60</RATE>< DETAIL><FR EIGHTCOST> $87.82</FR EIGHTCOST> <FUEL_SURC HARGE>$24. 78</FUEL_S URCHARGE>< /DETAIL><T RANSIT>1</ TRANSIT></ CARRIER><C ARRIER OPTIONID="9"><CARRIERNAME> ROADWAY EXPRESS</CARRIERNAME><SCAC >RDWY</SCA C><RATE>$1 24.85</RAT E><DETAIL> <FREIGHTCO ST>$103.15 </FREIGHTC OST><FUEL_ SURCHARGE> $21.70</FU EL_SURCHAR GE></DETAI L><TRANSIT >2</TRANSI T></CARRIE R><CARRIER OPTIONID="10"><CARRIERNAME >CENTRAL FREIGHT</CARRIERNAME><SCAC >CEDE</SCA C><RATE>$1 26.72</RAT E><DETAIL> <FREIGHTCO ST>$105.69 </FREIGHTC OST><FUEL_ SURCHARGE> $21.03</FU EL_SURCHAR GE></DETAI L><TRANSIT >2</TRANSI T></CARRIE R><CARRIER OPTIONID="11"><CARRIERNAME >WATKINS MOTOR LINES</CARRIERNAME><SCAC>W WAT</SCAC> <RATE>$131 .45</RATE> <DETAIL><F REIGHTCOST >$113.21</ FREIGHTCOS T><FUEL_SU RCHARGE>$1 8.24</FUEL _SURCHARGE ></DETAIL> <TRANSIT>2 </TRANSIT> </CARRIER> <CARRIER OPTIONID="12"><CARRIERNAME >UPS FREIGHT</CARRIERNAME><SCAC >OVNT</SCA C><RATE>$1 31.63</RAT E><DETAIL> <FREIGHTCO ST>$112.32 </FREIGHTC OST><FUEL_ SURCHARGE> $19.31</FU EL_SURCHAR GE></DETAI L><TRANSIT >1</TRANSI T></CARRIE R><CARRIER OPTIONID="13"><CARRIERNAME >FEDEX FREIGHT</CARRIERNAME><SCAC >GGGG</SCA C><RATE>$1 37.46</RAT E><DETAIL> <FREIGHTCO ST>$112.14 </FREIGHTC OST><GUARA NTEED_SERV ICE>$.00</ GUARANTEED _SERVICE>< FUEL_SURCH ARGE>$25.3 2</FUEL_SU RCHARGE></ DETAIL><TR ANSIT>1</T RANSIT></C ARRIER><CA RRIER OPTIONID="14"><CARRIERNAME >WATKINS GUARANTEED QUALITY SERVICE</CARRIERNAME><SCAC >WWAG</SCA C><RATE>$1 59.98</RAT E><DETAIL> <FREIGHTCO ST>$115.22 </FREIGHTC OST><GUARA NTEED_SERV ICE>$27.00 </GUARANTE ED_SERVICE ><FUEL_SUR CHARGE>$17 .76</FUEL_ SURCHARGE> </DETAIL>< TRANSIT>2< /TRANSIT>< /CARRIER>< CARRIER OPTIONID="16"><CARRIERNAME >ROADWAY EXPRESS (GUARANTEED DAY PM)</CARRIERNAME><SCAC>RDW P</SCAC><R ATE>$182.4 7</RATE><D ETAIL><FRE IGHTCOST>$ 103.37</FR EIGHTCOST> <GUARANTEE D_SERVICE> $55.00</GU ARANTEED_S ERVICE><FU EL_SURCHAR GE>$24.10< /FUEL_SURC HARGE></DE TAIL><TRAN SIT>2</TRA NSIT></CAR RIER></FQQ UOTE>
<?xml version="1.0" encoding="ISO-8859-1"?><FQ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How's this going?
dim minrateshipper
dim shipper
dim shippers(3)
shippers(0) = "CEDE"
shippers(1) = "RDWY"
shippers(2) = "DAFG"
minrateval = 9999999999999999 'a very large number
for each shipper in shippers
Set centralFreightNode = objXML.selectSingleNode("/
If NOT TypeName(centralFreightNod
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS
Set centralFreightNode = Nothing
if carrierrate < minrateval then
minrateval = carrierrate
minrateshipper = shipper
end if
end if
next
Set centralFreightNode = objXML.selectSingleNode("/
'Make sure the node was found
If NOT TypeName(centralFreightNod
'Node was found - now get the values you want from the XML
carrierRate = centralFreightNode.selectS
freightCost = centralFreightNode.selectS
arrivalNotification = centralFreightNode.selectS
residentialFee = centralFreightNode.selectS
liftGate = centralFreightNode.selectS
fuelSurcharge = centralFreightNode.selectS
transitTime = centralFreightNode.selectS
Set centralFreightNode = Nothing
Else
'Handle if not found
End If