我是XML和DOM的新手。我試圖從http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xmlDOM + XML Questions +發生了什麼?
獲取美元利率目前的代碼很好,但我沒有真正掌握髮生了什麼事情的幾個部分。
問題
在XML文件中我看到幾個「的xmlns」的標籤,我知道這些都是命名空間,但我從來沒有遇到過,你有兩個場景。當我創建我的名稱空間引用器時,爲什麼它是我使用的最後一個名稱空間?
當我選擇我的節點時,在我看來,要使用xDOM.selectNodes(「/ f:gesmes/f:gesmes/f:Cube」),但是這個和其他任何組合f:gesmes返回零(!)節點。我已經能夠訪問Cube節點的唯一方法是//表達式
爲什麼代碼無法在沒有錯誤處理過程的情況下工作?如果沒有在錯誤恢復下的代碼裏噴出一個「對象變量或與塊未設置」錯誤
我的代碼
Option Explicit
Private Sub run() ' run the whole operation'
Dim http_req As http_req: Set http_req = New http_req
Dim xDom As MSXML2.DOMDocument60
Set xDom = New MSXML2.DOMDocument60
Dim url As String: url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
xDom.async = False
xDom.Load url
Do Until xDom.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Call find_ClassElement(xDom)
End Sub
Private Sub find_ClassElement(xDom As MSXML2.DOMDocument60)
Dim ticker As String
Dim if_USD As String
Dim xDOM_element As MSXML2.IXMLDOMElement
Dim xDOM_attribute As MSXML2.IXMLDOMAttribute
Dim xDom_selection As MSXML2.IXMLDOMSelection
Dim xDOM_nodeList As MSXML2.IXMLDOMNodeList
xDom.setProperty "SelectionNamespaces", "xmlns:f='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'"
Set xDOM_nodeList = xDom.SelectNodes("//f:Cube")
For Each xDOM_element In xDOM_nodeList
On Error Resume Next
if_USD = xDOM_element.Attributes(0).text
On Error GoTo 0
If if_USD = "USD" Then
ticker = xDOM_element.Attributes(1).text
End If
Next
Debug.Print ticker
End Sub
+1我知道有一種更好的方式來使用XPath表達式來獲得美元利率。好答案! – 2014-09-24 13:48:49