ruby 将 nokogiri xml 转换为哈希

ruby converting nokogiri xml to hash

我正在尝试使用 Nokogiri 将 XML 转换为散列:

<eveapi version="2">
  <currentTime>2016-05-01 11:38:14</currentTime>
  <result>
    <characterID>93898118</characterID>
    <characterName>Ghitzarai</characterName>
    <race>Minmatar</race>
    <bloodlineID>4</bloodlineID>
    <bloodline>Brutor</bloodline>
    <ancestryID>24</ancestryID>
    <ancestry>Slave Child</ancestry>
    <corporationID>98012663</corporationID>
    <corporation>Dry Atomic Fusion</corporation>
  </result>
</eveapi>

# asume xml is the above XML
hash = {}
xml.xpath('//result').each do |row|
  hash[get_node_name:] = row.content
end

现在 row.name 将不起作用,因为只有 returns result 一次。

如何从子节点获取正确的名称?

"now row.name wont work cause that only returns result once. How to get the right names from the child nodes?"

result后添加/*得到所有个子元素<result>,不管子元素名称:

xml.xpath('//result/*').each do |row|