1

I have a XML document that looks something like this:

<permit>
  <id>1</id>
  <inspection>
    <amount>100</amount>
    <type>pool</type>  
  </inspection>
  <inspection>
    <amount>400</amount>
    <type>pluminbing</type>  
  </inspection>
</permit>
<permit>
  <id>2</id>
  <inspection>
    <amount>1500</amount>
    <type>roof</type>  
  </inspection>
  <inspection>
    <amount>1700</amount>
    <type>building</type>  
  </inspection>
</permit>

I know I can loop through the permits and out the id like so:

 REXML::XPath.each(doc, '//permit') do |permit|
   permit_hash = {:id => permit.elements['id'].text}
 end

But I can't seem to loop through each permit's inspections and finally get an array output such as:

 permits = [{:id => 1, :inspections => [{:amount => 100, :type => 'pool'}, {:amount =>   400, :type => 'plumbing'}]}, {:id => 2, :inspections => [{:amount => 1500, :type => 'roof'}, {:amount => 1700, :type => 'building'}]}]

Seems like everything I try I get all inspections rather than just the inspections for each permit.

Suggestions?

user1280971
  • 105
  • 1
  • 9

1 Answers1

1

I had the same problem. I tried this on my code and it worked. (I'm using yours though)

//create a new array
@myInspectionsAmounts=Array.new

//get the amount from the inspection
amount = permits.elements['inspection'].elements['amount'].text

@myInspectionAmounts.push(amount)

I'm sure you can put that in a loop to get all the inspections, but this is the method i tried and I got all the values i needed regardless of how deep they were down the xml tree.

Hope this helps! :)

Shimon Rachlenko
  • 5,469
  • 40
  • 51