给出以下
XML.
<?xml version="1.0" encoding="UTF-8"?> <xmldata> <Products> <ProductCode>M406789</ProductCode> <ProductID>858</ProductID> <ProductName>M406789 Ignition Box</ProductName> <ProductDescriptionShort><img alt="" src="/v/vspfiles/assets/images/alliance_small.jpg" align="right" />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