无法从XML节点获取文本

这可能是一个简单的问题,但使用xmlhttp,我怎样才能得到这个XML中的令牌节点的文本? 必须有比这更好的方法:

XML.FirstChild.NextSibling.FirstChild.FirstChild.FirstChild.FirstChild.NextSibling.Text 

 <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/"> <soap:Body> <GetToken2Response xmlns="webservice"> <GetToken2Result> <ResponseStatus> <ResponseCd>Fail or Success or Warning</ResponseCd> <ResponseMsg>string</ResponseMsg> <Version>string</Version> </ResponseStatus> <Token>string</Token> <Expiration>double</Expiration> <Valid>boolean</Valid> </GetToken2Result> </GetToken2Response> </soap:Body> </soap:Envelope> 

我把你的XML保存到一个文件中,然后运行这个程序,它给了我'string'作为令牌的值。

 Public Sub ReadToken() Dim strUrl As String Dim objDoc As Object Dim strToken As String strUrl = CurrentProject.Path & Chr(92) & "brettville.xml" Set objDoc = CreateObject("Msxml2.DOMDocument.3.0") objDoc.async = False objDoc.validateOnParse = True objDoc.Load strUrl strToken = objDoc.getElementsByTagName("Token").Item(0).Text Debug.Print "strToken: '" & strToken & "'" Set objDoc = Nothing End Sub 

已更新为包含名称空间。 不幸的是,我不知道如何处理实际示例中的xmlns =“webservice”xml …

 Sub Test() Dim sXML As String Dim xmlDoc As DOMDocument Dim xNodeResult As IXMLDOMNode Dim xNodeToken As IXMLDOMNode Set xmlDoc = New DOMDocument40 sXML = "<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/"">" & _ "<soap:Body>" & _ "<GetToken2Response>" & _ "<GetToken2Result>" & _ " <ResponseStatus>" & _ " <ResponseCd>Fail or Success or Warning</ResponseCd>" & _ " <ResponseMsg>string</ResponseMsg>" & _ " <Version>string</Version>" & _ " </ResponseStatus>" & _ " <Token>string</Token>" & _ " <Expiration>double</Expiration>" & _ " <Valid>boolean</Valid>" & _ "</GetToken2Result>" & _ "</GetToken2Response>" & _ "</soap:Body>" & _ "</soap:Envelope>" xmlDoc.validateOnParse = True xmlDoc.setProperty "SelectionNamespaces", _ "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" xmlDoc.LoadXML (sXML) If xmlDoc.parseError.reason <> "" Then Debug.Print "Parse error: " & xmlDoc.parseError.reason Exit Sub End If xmlDoc.setProperty "SelectionLanguage", "XPath" Set xNodeResult = xmlDoc.DocumentElement.SelectSingleNode( _ "/soap:Envelope/soap:Body/GetToken2Response/GetToken2Result") Debug.Print xNodeResult.XML Set xNodeToken = xNodeResult.SelectSingleNode("Token") If Not xNodeToken Is Nothing Then Debug.Print xNodeToken.nodeTypedValue Else Debug.Print "???" End If End Sub