解析XML消息 - Parsing XML message

- 此内容更新于:2016-02-01
主题:

我试图解析下面的XML:我想检索的信息是最低销售和最大购买价值,相关ID,发现:。我目前使用下面的XML:

原文:

I'm attempting to parse the following XML:

<marketstat><type id="18">
      <buy><volume>33000000</volume><avg>40.53</avg><max>65.57</max><min>6.55</min><stddev>26.61</stddev><median>58.56</median><percentile>65.57</percentile></buy>
      <sell><volume>494489</volume><avg>69.47</avg><max>69.47</max><min>69.47</min><stddev>0.00</stddev><median>69.47</median><percentile>69.47</percentile></sell>
      <all><volume>33494489</volume><avg>40.96</avg><max>69.47</max><min>6.55</min><stddev>26.77</stddev><median>58.56</median><percentile>6.55</percentile></all>
    </type><type id="19">
      <buy><volume>270000</volume><avg>1707.31</avg><max>3549.38</max><min>239.74</min><stddev>1554.26</stddev><median>239.75</median><percentile>3549.34</percentile></buy>
      <sell><volume>48599</volume><avg>24930.45</avg><max>29869.95</max><min>5200.00</min><stddev>9875.66</stddev><median>29869.93</median><percentile>5232.20</percentile></sell>
      <all><volume>280926</volume><avg>1957.07</avg><max>10750.00</max><min>239.74</min><stddev>3352.87</stddev><median>1874.31</median><percentile>239.74</percentile></all>
    </type></marketstat>
</evec_api>

The pieces of information that I want to retrieve are the minimum sell and maximum buy values, associated with the ID, found here: <sell><min>69.47</min></sell>.

I'm currently using the following to get the XML: marketData = Nokogiri::XML(open(api))

楼主:你的问题是什么?你需要告诉我们你的实际尝试解决这个问题,或者告诉我们所有的地方你搜索和解释为什么他们没有帮助。是你没有告诉我们你的研究或工作。读meta.stackoverflow.com/q/261592/128421,“最小的、完整的和可核查的例子”和“如何问”。

(原文:What is your question? You need to show us your actual attempt of solving the problem, or show us all the places you've searched and explain why they didn't help. As is you haven't showed us your research or effort. Read meta.stackoverflow.com/q/261592/128421, "Minimal, Complete, and Verifiable example" and "How to Ask".)

解决方案:
使用提取感兴趣的节点,然后将它们转换为年代和挑选你想要的价值。通往你的最低销售节点,或者如果你想使用速记,说“在文档的任何地方”,所以您可以指定所有最低销售节点,让所有的最大的购买。
原文:

Use xpath to pull out the nodes of interest, then convert them to Floats and pick the value you want. The path to your minimum sell node is /marketstat/type/sell/min, or if you want to use shorthand, // says "anywhere in the document", so you can specify just //sell/min to get all of the minimum sell nodes and //buy/max to get all of the maximum buys.

sells = market_data.xpath('//sell/min').map(&:content).map(&:to_f)
buys = market_data.xpath('//buy/max').map(&:content).map(&:to_f)

puts sells.min, buys.max
网友:两种答案是什么我需要的混合物。谢谢你!

(原文:A mixture of both answers is what I needed. Thank you.)

解决方案:
以下将打印ID和相应的最小/最大:输出:
原文:

The following will print the ID and its corresponding min/max:

marketData = Nokogiri::XML(open(api))
marketData.xpath("//type").each do |i|
    puts "#{i.attr('id')}: #{i.xpath('.//max').map {|j| j.text.to_f}.max}"
    puts "#{i.attr('id')}:  #{i.xpath('.//min').map {|j| j.text.to_f}.min}"
end

Output:

18: 69.47
18:  6.55
19: 29869.95
19:  239.74
网友:两种答案是什么我需要的混合物。谢谢你!

(原文:A mixture of both answers is what I needed. Thank you.)