XML的经典ASP - 空值

时间:2021-07-07 01:35:50

I have the following classic ASP code:

我有以下经典的ASP代码:

Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

    URL="The link to the XML source goes here"
    objXMLHTTP.Open "GET", URL, false
    objXMLHTTP.Send

    strHTML = objXMLHTTP.responseText

    'Load the XML File
    Set objXML = Server.CreateObject("Microsoft.XMLDOM")
    objXML.async = False
    objXML.loadXML(strHTML)

    Set objLst = objXML.getElementsByTagName("result")

strSearchCity = objLst(0).getElementsByTagName("adminArea5")(0).firstchild.nodeValue
strSearchState = objLst(0).getElementsByTagName("adminArea3")(0).firstchild.nodeValue

Here is a snip-it of the XML that works fine:

这是一个可以正常运行的XML的剪辑:

<response>
<info>
</info>
<results>
   <result>
      <providedLocation>
         <location>chicago, il</location>
      </providedLocation>
      <locations>
         <location>
             <adminArea5 type="City">Chicago</adminArea5>
             <adminArea3 type="State">IL</adminArea3>
             <adminArea4 type="County">Cook County</adminArea4>
         </location>
      </locations>
   </result>
</results>
</response>

However, if one of these comes back null, I get an error.

但是,如果其中一个返回null,我会收到错误。

<location>
    <adminArea5 type="City">Washington</adminArea5>
    <adminArea3 type="State"/>
</location>

I apologize, as I'm sure this will be easy, but how do I prevent the error from occuring? Do I check to see if the value is null? Do I grab the values in a different way?

我道歉,因为我相信这很容易,但我如何防止错误发生?我是否检查该值是否为空?我是否以不同的方式获取值?

Thank you.

Rick

1 个解决方案

#1


0  

firstChild.nodeValue requires the firstChild property to be not-null, because null doesn't have nodeValue property.

firstChild.nodeValue要求firstChild属性不为null,因为null没有nodeValue属性。

The simpler way to avoid that error is to use innerText instead of firstChild.nodeValue :

避免该错误的更简单方法是使用innerText而不是firstChild.nodeValue:

strSearchState = objLst(0).getElementsByTagName("adminArea3")(0).innerText

#1


0  

firstChild.nodeValue requires the firstChild property to be not-null, because null doesn't have nodeValue property.

firstChild.nodeValue要求firstChild属性不为null,因为null没有nodeValue属性。

The simpler way to avoid that error is to use innerText instead of firstChild.nodeValue :

避免该错误的更简单方法是使用innerText而不是firstChild.nodeValue:

strSearchState = objLst(0).getElementsByTagName("adminArea3")(0).innerText