2011-08-26 106 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”命名空间的异常,这似乎打破它...查看更新的答案。 –