用Excel查询联机XML文档
从excel文档查询以从live XML数据源更新(我无论如何都无法编辑或更改) 来自web站点的XML代码用Excel查询联机XML文档,xml,excel,vba,Xml,Excel,Vba,从excel文档查询以从live XML数据源更新(我无论如何都无法编辑或更改) 来自web站点的XML代码 <?xml version='1.0' encoding='utf-8'?> <evec_api version="2.0" method="marketstat_xml"> <marketstat><type id="24692"> <buy><volume>58</volume><
<?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>
尝试查询=GetPrice(24692,“//sell/min”)时,excel返回#值!错误。查询=GetPrice(24692,“卖出”)时将返回66211618794.64266968892.26202896968.0011782399.14206970995.32202896977.39
当我想查询202896968.00时,它似乎是来自销售行的所有数据,销售行数据中的标记为min,购买标记中的最大值用于填充Excel文档。getElementsByTagName方法只能使用单个标记名,不能使用XPath查询。如果检查具有子节点(例如“sell”节点)的节点的
Text
属性,则可以获得上下文节点及其所有子节点的文本内容
尝试改用selectNodes
,并将XPath查询传递给函数,如下所示:
GetPrice=xmresp.selectNodes(sItem).Item(0).Text
并使用您原来的
=GetPrice(24692,//sell/min)
公式这对我很有效。使用DOMDocument方法。首先,设置对MicrosoftXML V6.0的引用。然后,以下代码将检索“Buy”“Max”的当前值
您可以展开、修改以检索所需的任何标记
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
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