2014-01-13 87 views
3

從excel文檔查詢以從活動XML數據源進行更新(我無法編輯或更改)。使用Excel查詢在線XML文檔

從網站的XML代碼

<?xml version='1.0' encoding='utf-8'?> 
<evec_api version="2.0" method="marketstat_xml"> 
    <marketstat><type id="24692"> 
     <buy><volume>58</volume><avg>187454397.24</avg><max>191102293.00</max><min>170620000.01</min><stddev>6655174.57</stddev><median>191000100.02</median><percentile>191102293.00</percentile></buy> 
     <sell><volume>66</volume><avg>211618794.64</avg><max>266968892.26</max><min>202896968.00</min><stddev>11782399.14</stddev><median>206970995.32</median><percentile>202896977.39</percentile></sell> 
     <all><volume>224</volume><avg>110889266.01</avg><max>266968892.26</max><min>0.99</min><stddev>30116759.19</stddev><median>186251523.01</median><percentile>0.99</percentile></all> 
    </type></marketstat> 
</evec_api> 

我的UDF的excel模塊代碼

Function GetPrice(sItemID As String, sItem As String, Optional sURL = "", Optional sSystem = "") As Variant 
Dim oHttp As New MSXML2.XMLHTTP60 
Dim xmlResp As MSXML2.DOMDocument60 
Dim result As Variant 
On Error GoTo EH 

If sURL = "" Then 
    sURL = "http://api.eve-central.com/api/marketstat?typeid=" 
End If 

If sSystem = "" Then 
    sSystem = "&usesystem=30000142" 
End If 

'open the request and send it 
oHttp.Open "GET", sURL & sItemID & sSystem, False 
oHttp.Send 

'get the response as xml 
Set xmlResp = oHttp.responseXML 
' get Item 
GetPrice = xmlResp.getElementsByTagName(sItem).Item(0).Text 

' Examine output of these in the Immediate window 
Debug.Print sName 
Debug.Print xmlResp.XML 

CleanUp: 
On Error Resume Next 
Set xmlResp = Nothing 
Set oHttp = Nothing 
Exit Function 
EH: 
GetPrice = CVErr(xlErrValue) 
GoTo CleanUp 
End Function 

當試圖查詢=用getPrice(24692, 「//賣出/分鐘」)練成返回#值!錯誤。當查詢= GetPrice(24692,「賣」)將返回66211618794.64266968892.26202896968.0011782399.14206970995.32202896977.39 這似乎是來自銷售行的所有數據,當我正在尋找202896968.00與銷售行數據標籤下min以及buy標籤中的最大值來填充Excel文檔。

回答

1

getElementsByTagName方法只能使用單個標記名稱而不使用XPath查詢。如果您檢查具有子節點的節點(例如「賣」節點)的Text屬性,那麼您將獲取上下文節點及其所有子節點的文本內容。

嘗試使用selectNodes而不是和XPath查詢傳遞給你的函數,像這樣:

GetPrice = xmlResp.selectNodes(sItem).Item(0).Text

,並使用你原來的=GetPrice(24692, "//sell/min")公式

0

這爲我工作。使用DOMDocument方法。首先,設置對Microsoft XML V6.0的引用。然後將下面的代碼將檢索「購買」,「最大」

Set xmlDoc = CreateObject("MSXML2.DOMDocument") 
    xmlDoc.Load ("http://api.eve-central.com/api/marketstat?typeid=24692&usesystem=30000142") 

    buy_max = xmlDoc.getElementsByTagName("buy")(0).getElementsByTagName("max")(0).Text 

可以擴大現有的值,修改檢索你需要的任何標籤