Hans de Jong
Hans de Jong

Reputation: 2078

ruby converting nokogiri xml to hash

I am trying to convert XML with Nokogiri to a hash:

<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

Now row.name won't work cause that only returns result once.

How do I get the right names from the child nodes?

Upvotes: 0

Views: 1069

Answers (1)

har07
har07

Reputation: 89285

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

Add /* after result to get all child elements of <result> regardless of the child element name :

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

Upvotes: 2

Related Questions