2016-08-18 181 views
0

我正嘗試將一個MS Access數據庫連接到SOAP服務。VBA和SOAP接口

我能夠連接到服務,檢索XML,但我無法讀取節點值。

這是我的代碼(我換成++++++一些機密的東西):

Option Compare Database 

Private Sub lanzar_XML_Click() 
    Dim sUrl As String 
    Dim sEnv As String 
    Dim xmlhtp As MSXML2.XMLHTTP60 
    Dim xmlDoc As MSXML2.DOMDocument60 
    Dim limpiartexto As String 
    Dim Valoresxml As MSXML2.IXMLDOMNode 

    sUrl = "++++++" 
    sEnv = "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:hai=""++++++"">" 
    sEnv = sEnv & " <soapenv:Header/>" 
    sEnv = sEnv & " <soapenv:Body>" 
    sEnv = sEnv & "  <hai:QuerySRInfo>" 
    sEnv = sEnv & "   <SRNum>EUES160624000378</SRNum>" 
    sEnv = sEnv & "   <ServiceCenterId>++++++</ServiceCenterId>" 
    sEnv = sEnv & "   <ServiceCenterPW>++++++</ServiceCenterPW>" 
    sEnv = sEnv & "  </hai:QuerySRInfo>" 
    sEnv = sEnv & " </soapenv:Body>" 
    sEnv = sEnv & "</soapenv:Envelope>" 
    Set xmlhtp = New MSXML2.XMLHTTP60 
    With xmlhtp 
     .Open "POST", sUrl, False 
     .setRequestHeader "Content-Type", "text/xml;charset=UTF-8" 
     ' .setRequestHeader "SOAPAction:", "rpc/http://www.++++++.com/:QuerySRInfo" 
     .setRequestHeader "Content-Length", "Lenght" 
     .send sEnv 

     Set xmlDoc = New MSXML2.DOMDocument60 
     xmlDoc.loadXML .responseText 

     limpiartexto = xmlDoc.XML 
     limpiartexto = Right(limpiartexto, Len(limpiartexto) - 327) 
     limpiartexto = Left(limpiartexto, Len(limpiartexto) - 64) 

     Debug.Print limpiartexto 
    End With 

    xmlDoc.loadXML limpiartexto 

    For Each Valoresxml In xmlDoc.getElementsByTagName("ServiceRequests") 
     Me.MobilePhone = Valoresxml.selectNodes("MobilePhone")(0).Text 
     MsgBox Valoresxml.selectNodes("MobilePhone")(0).Text 
    Next 
End Sub 

我相信我的問題是,而不是一個結構良好的XML與檢索到的XML格式,它只是一個包含所有節點的單線程。

debug.print回報:

<ServiceRequests><ServiceRequest xmlns="http://www.siebel.com/xml/SPI%20HET%20ASC%20Query%20Service%20Request"><SrNumber>EUES160624000378</SrNumber><CustomerName>++++++</CustomerName><FirstName>++++++</FirstName><LastName>++++++</LastName><ContactEmail></ContactEmail><MobilePhone>600600600</MobilePhone><HomePhone></HomePhone><WorkPhone></WorkPhone><CustomerGender></CustomerGender><CustomerCoutnry>Spain</CustomerCoutnry><CustomerState></CustomerState><SubmissionTime></SubmissionTime><OfflineSubmissionTime></OfflineSubmissionTime></ServiceRequest></ServiceRequests> 

...但我認爲它應該返回:

<ServiceRequests> 
<ServiceRequest xmlns="http://www.siebel.com/xml/SPI%20HET%20ASC%20Query%20Service%20Request">  
    <SrNumber>EUES160624000378</SrNumber> 
    <CustomerName>++++++</CustomerName> 
    <FirstName>++++++</FirstName> 
    <LastName>++++++</LastName> 
    <ContactEmail></ContactEmail> 
    <MobilePhone>600600600</MobilePhone> 
    <HomePhone></HomePhone> 
    <WorkPhone></WorkPhone> 
    <CustomerGender></CustomerGender> 
    <CustomerCoutnry>Spain</CustomerCoutnry> 
    <CustomerState></CustomerState> 
    <SubmissionTime></SubmissionTime> 
    <OfflineSubmissionTime></OfflineSubmissionTime> 
</ServiceRequest> 
</ServiceRequests> 

我該如何處理呢?

注意:當我使用soapui時,返回的XML看起來很好。

+0

XML/MSXML不關心換行符。那是什麼問題?這是怎麼回事? –

+2

[如何使用vba解析XML]可能的重複(http://stackoverflow.com/questions/11305/how-to-parse-xml-using-vba) – trincot

+0

我標記爲重複,因爲您不應該「解析「字符串函數如」Left「和」Right「的XML。相反,使用DOM API。這種間距(或缺乏)變得無關緊要。該API負責所有這些。 – trincot

回答

0

響應XML有一個未聲明的名稱空間,應該用任何字符串前綴聲明(以下使用doc)以引用基礎元素。在任何節點路徑引用中使用此前綴。然後,使用nodeList進行迭代。當然這假定limpiartextoLeft()Right()處理後呈現有效的XML。

Dim XMLNamespaces As String     ' among other references 
Dim xmlNodeList As MSXML2.IXMLDOMNodeList 
... 
xmlDoc.loadXML limpiartexto 
XMLNamespaces = "xmlns:doc='http://www.siebel.com/xml/SPI%20HET%20ASC%20Query%20Service%20Request'" 
XmlDoc.setProperty "SelectionNamespaces", XMLNamespaces 

Set xmlNodeList = xmlDoc.SelectNodes("//doc:ServiceRequest") 

For Each Valoresxml In xmlNodeList 
    Me.MobilePhone = Valoresxml.SelectSingleNode("doc:MobilePhone").Text 
    MsgBox Valoresxml.SelectSingleNode("doc:MobilePhone").Text 
Next Valoresxml