2011-08-26 185 views
1

這可能是一個簡單的問題,但使用xmlhttp,如何獲取此XML中的令牌節點的文本?必須有比這更好的方式:無法從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> 
+1

老兄,你總得* *退房的XPath ......這'.FirstChild.NextSibling.FirstChild.FirstChild.FirstChild ...'業務是無法控制的! –

回答

0

我救了你的XML文件,然後運行這個程序,它給了我'string'作爲Token的值。

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 
1

更新,包括命名空間。不幸的是我無法弄清楚如何處理在您的實際示例XML使用了「xmlns =‘web服務’」 ......

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 
+0

我嘗試使用XPath,但也許我在搜索字符串錯誤。起初,我嘗試了/ GetToken2Response/GetToken2Result/Token,它返回一個空白字符串。我知道XML令牌不是空白的,因爲我仍然可以使用FirstChild.NextSibling混亂獲取令牌。在我的問題中,我的XML選擇字符串看起來像我的XML?謝謝! – DontFretBrett

+0

由於名稱空間(我之前沒有意識到的一個問題,因爲這不是我的專業知識領域......),所以您的示例稍微複雜一點。我「有點」讓它與「webservice」命名空間的異常,這似乎打破它...查看更新的答案。 –