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模式场景研究。
我想实现以下目标:
读取节点是否为“fedex” 如果是,则计算存在多少节点 如果count> 0,则循环进入以获取每个节点的值(例如:shippingMethod,TotalRate) 如果count = 0,则什么都不做。
读取节点是否为“CUSTOM” 如果是,那么算一下里面有多少 如果count> 0,则循环进入以获取每个节点的值(例如:shippingmethod,TotalRate) 如果count = 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
任何人都知道完整的解决方案吗?
答案 0 :(得分:3)
让我们假设您已整理了该XML,以便它包含根节点,并且属性值包含在“”中。我的猜测是你真正追求的是: -
Dim oXML: Set oXML = CreateObject("MSXML2.DOMDocument.3.0")
oXML.loadXML(myXML) '' # to load from string directly
Dim iItem : iItem = 0
Dim shippingMethod, totalRate
Dim strItemLine : strItemLine = ""
Dim rate
For Each rate In oXML.documentElement.selectNodes("shippingRates/shippingRate")
shippingMethod = rate.selectSingleNode("shippingMethod").Text
totalRate = rate.selectSingleNode("TotalRate").Text
strItemLine = strItemLine & "&" & Server.URLEncode("L_SHIPPINGOPTIONNAME" & iItem) & "=" & Server.URLEncode(shippingMethod)
strItemLine = strItemLine & "&" & Server.URLEncode("L_SHIPPINGOPTIONAMOUNT" & iItem) & "=" & Server.URLEncode(totalRate)
iItem = iItem + 1
Next
从您提供的代码中,Fedex和CUSTOM之间没有实际的区别,因此代码显着简化。