amareshray
amareshray

Reputation: 1

REXML - Storing a certain element

This is my XML file:

<performance_summary_response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://cakemarketing.com/affiliates/api/2/">
  <success>true</success>
  <row_count>7</row_count>
  <periods>
    <period>
      <date_range>12 Months</date_range>
      <current_revenue>22369.7000</current_revenue>
      <previous_revenue>3664.1000</previous_revenue>
      <currency_symbol>$</currency_symbol>
    </period>
  </periods>
</performance_summary_response>

I'm using REXML to extract the current_revenue element where the date_range is 12 months. I've got the following code:

period = ['12 Months']

data.elements.each('//date_range/..') do |parent|
  if (period.include? parent.elements['date_range/text()'].to_s)
    puts parent.elements['current_revenue']

  end

end

This prints 22369.7000, however, I'm unable to extract just the numerical value and assign this to a variable. How could I possibly go about doing that?

Upvotes: 0

Views: 42

Answers (1)

ramblex
ramblex

Reputation: 3057

For me puts parent.elements['current_revenue'] prints <current_revenue>22369.7000</current_revenue>. If you want just 22369.7000, you can use:

 current_revenue = parent.elements['current_revenue'].text

current_revenue will be a string, so you can use to_f or a variant thereof to convert it to a number.

Upvotes: 1

Related Questions