如何从XML API检索特定值?

我有一个API。

http://eve-marketdata.com/api/item_history2.xml?char_name=demo&region_ids=10000002&type_ids=33468&days=2

我只想要“音量”值。

在Excel 2013中,我使用了

=FILTERXML(WEBSERVICE(API ADDRESS),"//row/@volume") 

但我得到#VALUE! 错误。

有人能指出我做错了什么吗?


题:

当我把这个公式放在:

 =FILTERXML(WEBSERVICE("http://eve-marketdata.com/api/item_history2.xml?char_name=demo&region_ids=10000002&type_ids=33468&days=2"), "//emd/result/rowset/row/@volume") 

我得到了#VALUES! 我认为问题是来自web服务function本身,因为当我把这个公式:

 =WEBSERVICE(http://eve-marketdata.com/api/item_history2.xml?char_name=demo&region_ids=10000002&type_ids=33468&days=2) 

我也得到#VALUE !. 你知道问题是什么吗?

您需要微调您的Xpath查询。 看看Web服务返回的XML:

 <?xml version="1.0" encoding="utf-8"?> <emd version="2"> <currentTime>2016-01-29 13:37:47</currentTime> <result> <rowset name="history" key="typeID,regionID,date" columns="typeID,regionID,date,lowPrice,highPrice,avgPrice,volume,orders"> <row typeID="33468" regionID="10000002" date="2016-01-28" lowPrice="74500000.06" highPrice="76300002.15" avgPrice="76248999.69" volume="279" orders="219"/> <row typeID="33468" regionID="10000002" date="2016-01-29" lowPrice="74000000.89" highPrice="80449888.7" avgPrice="75000104.61" volume="101" orders="68"/> </rowset> </result> </emd> 

您的Xpath查询需要将您从文档的根目录转到您感兴趣的值,并以完整的path,因此您需要:

 //emd/result/rowset/row/@volume 

把这一切放在一起,你的公式应该是:

 =FILTERXML(WEBSERVICE("http://eve-marketdata.com/api/item_history2.xml?char_name=demo&region_ids=10000002&type_ids=33468&days=2"), "//emd/result/rowset/row/@volume") 

这给了你279的结果 – 因为它只取第一个值。

为了得到两个值,你需要对FILTERXML函数进行多次调用,并调整你的Xpath来考虑这个问题。 如果你这样做,我会build议移动到WEBSERVICE调用到自己的单元格。

所以,你的最终结果可能看起来像这样:

 =WEBSERVICE("http://eve-marketdata.com/api/item_history2.xml?char_name=demo&region_ids=10000002&type_ids=33468&days=2") =FILTERXML(A1, "//emd/result/rowset/row[position()=1]/@volume") =FILTERXML(A1, "//emd/result/rowset/row[position()=2]/@volume") 

请注意,由于某种原因, position()从1开始计数。