带有MSXML6和DOMDocument60的getElementsByTagName

时间:2018-06-20 10:52:37

标签: getelementsbytagname msxml6

我正在将我的项目从MSXML4升级到MSXML6,但在查找如何正确地将getElementsByTagName与DOMDocument60结合使用时遇到麻烦。

我正在使用与此类似的代码来调用Web服务...

Dim objDOM As New MSXML2.DOMDocument60
Dim objXMLHTTP As New MSXML2.XMLHTTP60
Dim objResponse As MSXML2.DOMDocument60

Dim XMLStr As String
XMLStr = "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf
XMLStr = XMLStr & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
XMLStr = XMLStr & "<soap:Body>"
XMLStr = XMLStr & "<RefreshUserExpiryInformation xmlns=""http://localhost/myservices/"">"
XMLStr = XMLStr & "<EncryptedCheckKey>2FEE026</EncryptedTBSSerialNo>"
XMLStr = XMLStr & "</RefreshUserExpiryInformation>"
XMLStr = XMLStr & "</soap:Body>"
XMLStr = XMLStr & "</soap:Envelope>"

With objDOM
    .async = False
    .loadXML XMLStr
End With

With objXMLHTTP
    .Open "POST", "http://localhost/myservices/TBSV1.asmx", False
    .setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    .setRequestHeader "SoapAction", "http://localhost/myservices/RefreshUserExpiryInformation"
    .send objDOM.XML
End With

Set objDOM = Nothing
Set objResponse = objXMLHTTP.responseXML

我调试结果时的此响应文本(ResponseText)...

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <RefreshUserExpiryInformationResponse xmlns="http://localhost/myservices/">
      <RefreshUserExpiryInformationResult>2020-06-30T00:00:00</RefreshUserExpiryInformationResult>
    </RefreshUserExpiryInformationResponse>
  </soap:Body>
</soap:Envelope>

使用MSXML4,我曾经做过类似的事情...

MsgBox objResponse.getElementsByTagName("RefreshUserExpiryInformationResult").Item(0).Text

使用MSXML6和DOMDocument60声明时,getElementsByTagName返回“对象变量或未设置块变量”。

我读过很多文章,提到可能与名称空间有关,请将其添加到正确的位置。我尝试在objDOM和objXML上添加名称空间,但无法弄清楚。

0 个答案:

没有答案