vbscript遍历xml节点 - 动态if else,节点数检测

vbscript遍历xml节点 - 动态if else,节点数检测

问题描述:

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 

任何人都知道一个完整的解决方案吗?

+0

为什么URLEncode,你确定你不应该使用HTMLEncode?你对最后的字符串做什么?你似乎没有对strItemLine做任何事情? – AnthonyWJones 2009-09-04 10:56:09

+0

另外你的XML格式很糟糕,你只能在文件顶层有一个节点 – AnthonyWJones 2009-09-04 11:00:03

假设您已经整理了该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 

从你的代码提供有联邦快递和CUSTOM因此次代码显著简化之间没有实际disctinction。

+0

你好,原因是这样的,fedex的输出有时会有速度,有时会没有速度。如果未找到费率,将显示。不过,我需要将两个结果合并为strItemLine,这是一个完整的字符串 - 我可以发布到其他网站服务。 – 2009-09-04 12:29:13

+0

该点上的速率节点很可能没有“shippingMethod”或“TotalRate”元素,请检查您的XML并注意比较区分大小写。 – AnthonyWJones 2009-09-05 11:12:14