加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

检查xml节点是否不存在并执行某些操作而不是失败

发布时间:2020-12-16 23:21:37 所属栏目:百科 来源:网络整理
导读:给出以下 XML. ?xml version="1.0" encoding="UTF-8"?xmldata Products ProductCodeM406789/ProductCode ProductID858/ProductID ProductNameM406789 Ignition Box/ProductName ProductDescriptionShortlt;img alt="" src="/v/vspfiles/assets/images/allian
给出以下 XML.

<?xml version="1.0" encoding="UTF-8"?>
<xmldata>
   <Products>
       <ProductCode>M406789</ProductCode>
       <ProductID>858</ProductID>
       <ProductName>M406789 Ignition Box</ProductName>
       <ProductDescriptionShort>&lt;img alt="" src="/v/vspfiles/assets/images/alliance_small.jpg" align="right" /&gt;Ignition Box</ProductDescriptionShort>
       <ListPrice>134.2200</ListPrice>
       <ProductPrice>80.5300</ProductPrice>
       <SalePrice>59.9500</SalePrice>
   </Products>
</xmldata>

这是脚本的相关部分.

Set xNewDoc = xData.responseXML 'ResponseXml returns DOMDocument object

Set ProductCode = xNewDoc.SelectSingleNode("//ProductCode")
Set ListPrice = xNewDoc.SelectSingleNode("//ListPrice")

x=Len(ListPrice.Text)
newlp = Left(ListPrice.Text,x-2)

Set ProductPrice = xNewDoc.SelectSingleNode("//ProductPrice")
x=Len(ProductPrice.Text)
newpp = Left(ProductPrice.Text,x-2)

Set SalePrice = xNewDoc.SelectSingleNode("//SalePrice")
x=Len(SalePrice.Text)
newsp = Left(SalePrice.Text,x-2)

Set ProductName = xNewDoc.SelectSingleNode("//ProductName")

如果缺少上面加载的xml和节点(假设为“SalePrice”),则脚本将失败.如何测试以查看节点是否存在以使其不会失败.我过去在Stack上看过这个东西,但似乎无法找到它.

解决方法

设置从XML获取节点后,对其余部分应用“无任务”检查:

newpp = 0 'Initialize with a default value
Set ProductPrice = xNewDoc.SelectSingleNode("//ProductPrice")
If Not ProductPrice Is Nothing Then
    x=Len(ProductPrice.Text)
    newpp = Left(ProductPrice.Text,x-2)
End If

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读