2017-05-26 49 views
0

我無法弄清楚,爲什麼我的代碼是不承認的變量名「HourlySchedule」 XML響應文本。當它到達:For Each HourlySchedule In Resp.getElementsByTagName("HourlySchedule"),它會跳到結束,而不是循環直通每個標籤。我試過幾個不同的標籤名稱,但它似乎不起作用。有什麼建議麼?getElementsByTagName來不工作

我的VBA代碼:

Sub Button1_Click() 
    Dim URL As String: URL = "webaddress here" 
    Dim mfile As String 
    mfile = "<?xml version=" & """" & "1.0" & """" & "?><Envelope xmlns=" & """" & "http://schemas.xmlsoap.org/soap/envelope/" & """" & "><Header/><Body><QueryRequest xmlns=" & """" & "http://markets.midwestiso.org/dar/xml" & """" & "><QueryMarketResults day=" & """" & "2017-03-05" & """" & "><LocationName>Rug</LocationName></QueryMarketResults></QueryRequest></Body></Envelope>" 

    Set Req = New WinHttp.WinHttpRequest 
    With Req 
     .Open "POST", URL, False 
     .SetClientCertificate "CURRENT_USER\MY\name" 
     .SetRequestHeader "content-type", "text/xml" 
     .Send (mfile) 
     .WaitForResponse 
    End With 

    Dim Resp As New MSXML2.DOMDocument60 
    Resp.LoadXML Req.ResponseText 

if Resp.loadxml (Req.ResponseText) then 
    MsgBox "ok" else 
    MsgBox "err" 
end if 

    Dim HourlySchedule As IXMLDOMNode 
    For Each HourlySchedule In Resp.getElementsByTagName("HourlySchedule") ''this is where my problem is 
    Debug.Print "test" 
    Next HourlySchedule 
    End Sub 

這裏是我試圖解析XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 
    <Body> 
     <QueryResponse xmlns="http://markets.midwestiso.org/dart/xml"> 
      <MarketResults day="2017-03-05"> 
       <Location name="OTP.RUGBY1_IBR"> 
        <HourlySchedule hour="1"> 
         <ClearedEnergy MW="-6" virtualMW="0" price="7" capped="false"/> 
         <ClearedReg MW="0" price="10.18" capped="false"/> 
         <ClearedSpin MW="0" price="1" capped="false"/> 
         <ClearedSupp MW="0" price="0.5" capped="false"/> 
         <ClearedRampCapabilityUp MW="0" price="0"/> 
         <ClearedRampCapabilityDown MW="0" price="0"/> 
        </HourlySchedule> 
        <HourlySchedule hour="2"> 
         <ClearedEnergy MW="-2" virtualMW="0" price="5.3" capped="false"/> 
         <ClearedReg MW="0" price="8.06" capped="false"/> 
         <ClearedSpin MW="0" price="1" capped="false"/> 
         <ClearedSupp MW="0" price="0.5" capped="false"/> 
         <ClearedRampCapabilityUp MW="0" price="0"/> 
         <ClearedRampCapabilityDown MW="0" price="0"/> 
        </HourlySchedule> 
       </Location> 
      </MarketResults> 
     </QueryResponse> 
    </Body> 
</Envelope> 
+0

'myfile'是任何東西,但不是XML。請製作一個語法上有效的VB字符串或者包含語法上有效的XML - 或者將XML文件放入單獨的代碼塊中以供參考。 – Tomalak

+0

最可能的解釋是,有分析錯誤,並沒有真正加載的響應文件。檢查['parseError'屬性](https://msdn.microsoft.com/en-us/library/ms756041(v = vs.85).aspx)(實際上你總是會在加載後檢查解析錯誤一個XML文檔)。在更普遍的情況下,而不是'WinHttp.WinHttpRequest',我建議使用'MSXML2.XMLHTTP60'請求對象,因爲這會透明地做XML解析你(只是訪問其['responseXml'財產(https://開頭msdn.microsoft.com/en-us/library/ms757066(v=vs.85).aspx))。 – Tomalak

+0

感謝您的反饋,我對解析XML有點新。你能舉一個例子,說明如何讓myfile語法上有效的VB字符串?謝謝。 – guice99

回答

0

一個經常被提及的問題在解析XML文檔是一個未聲明的命名空間前綴,你的響應包含兩次在不同的節點級別。通知沒有冒號分隔名稱被包括是完全有效的XML:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 

<QueryResponse xmlns="http://markets.midwestiso.org/dart/xml"> 

其結果是,在VBA通過指定用戶定義的前綴,這裏DOCDOC2用於聲明這樣的命名空間。然後,使用SelectNodes方法在getElementsByTagName,因爲你將需要參考所述第二定義的前綴作爲HourlySchedule查詢節點的子節點,然後可以查詢到所需要的元件:

... 
    Dim HourlySchedule As IXMLDOMNode  
    Dim XmlNamespaces As String 

    ' SPACE SEPARATED STRING 
    XmlNamespaces = "xmlns:doc='http://schemas.xmlsoap.org/soap/envelope/'" _ 
        & " xmlns:doc2='http://markets.midwestiso.org/dart/xml'" 
    Resp.setProperty "SelectionNamespaces", XmlNamespaces 

    For Each HourlySchedule In Resp.DocumentElement.SelectNodes("//doc2:HourlySchedule") 
     Debug.Print "test" 
    Next HourlySchedule 

    Set Resp = Nothing 

End Sub 

輸出(在立即窗口中)

test 
test 
+0

完美,非常感謝你,我永遠不會想到這一點。 – guice99

+0

太棒了!很高興我能幫上忙。是的,對於新的XML用戶命名空間可以是一個非常微妙的忽視。請接受解決方案(勾選標記到旁邊)以確認解決方案。 – Parfait