myXML= "<?xml version=1.0 encoding=iso-8859-1 ?>" & vbcrlf & _
"<shippingRates code=fedex >" & vbcrlf & _
"<errorMsg>" & vbcrlf & _
"Sorry, no rates returned." & vbcrlf & _
"</errorMsg>" & vbcrlf & _
"</shippingRates>" & vbcrlf & _
"<shippingRates code=CUSTOM >" & vbcrlf & _
"<shippingRate index=0 >" & vbcrlf & _
"<TotalRate>0.29</TotalRate>" & vbcrlf & _
"<HandlingFee>0.00</HandlingFee>" & vbcrlf & _
"<DisplayHandlingFeeOpt>1</DisplayHandlingFeeOpt>" & vbcrlf & _
"<shippingMethod>shipping option 1 </shippingMethod>" & vbcrlf & _
"</shippingRate>" & vbcrlf & _
"<shippingRate index=1 >" & vbcrlf & _
"<TotalRate>2.91</TotalRate>" & vbcrlf & _
"<HandlingFee>43.69</HandlingFee>" & vbcrlf & _
"<DisplayHandlingFeeOpt>1</DisplayHandlingFeeOpt>" & vbcrlf & _
"<shippingMethod>shipping option 2 </shippingMethod>" & vbcrlf & _
"</shippingRate>" & vbcrlf & _
"</shippingRates>" & vbcrlf
Dim oXML: Set oXML = Server.CreateObject("Microsoft.XMLDOM")
oXML.loadXML(myXML) 'to load from string directly
基於xml模式情景研究。vbscript遍歷xml節點 - 動態if else,節點數檢測
我想實現以下目標:
讀取如果節點是「聯邦快遞」 如果是的話,算多少節點存在 如果計數> 0,則循環到讓每個節點的值內(例如:shippingMethod,TotalRate) 如果count = 0,則什麼也不做。
讀如果節點是「自定義」 如果是,則計算有多少內部 如果計數> 0,則循環到進去的每個節點的值(例如:shippingmethod,TotalRate) 如果計數= 0,沒做什麼。
iItem= 0
set shippingRates_node = oXML.getElementsByTagName("shippingRates")
for each itemNodes in shippingRates_node(0).ChildNodes
set shippingRate_node = oXML.getElementsByTagName("shippingRate")
if code= "fedex" then
how to count?
if count>0 then
for each item in itemNodes.ChildNodes
if item.nodeName = "shippingMethod" Then
strItemLine= strItemLine & "&" & Server.URLEncode("L_SHIPPINGOPTIONNAME" & iItem) & "=" & Server.URLEncode(item.Text)
end if
if item.nodeName = "shippingRate" Then
strItemLine= strItemLine & "&" & Server.URLEncode("L_SHIPPINGOPTIONAMOUNT" & iItem) & "=" & Server.URLEncode(item.Text)
end if
next
iItem= iItem + 1
end if
end if
if code= "CUSTOM" then
how to count?
if count>0 then
for each item in itemNodes.ChildNodes
if item.nodeName = "shippingMethod" Then
strItemLine= strItemLine & "&" & Server.URLEncode("L_SHIPPINGOPTIONNAME" & iItem) & "=" & Server.URLEncode(item.Text)
end if
if item.nodeName = "shippingRate" Then
strItemLine= strItemLine & "&" & Server.URLEncode("L_SHIPPINGOPTIONAMOUNT" & iItem) & "=" & Server.URLEncode(item.Text)
end if
next
iItem= iItem + 1
end if
end if
Next
TotalShippingOptions= iItem
任何人都知道一個完整的解決方案嗎?
爲什麼URLEncode,你確定你不應該使用HTMLEncode?你對最後的字符串做什麼?你似乎沒有對strItemLine做任何事情? – AnthonyWJones 2009-09-04 10:56:09
另外你的XML格式很糟糕,你只能在文件頂層有一個節點 – AnthonyWJones 2009-09-04 11:00:03