0

I'm using XmlSimple to turn an XML document into a ruby hash. It turns data like this:

<resultPage>
  <total>2</total>
  <offset>0</offset>
  <limit>200</limit>
</resultPage>

Into this:

{ :resultPage => [
    {
      :offset => [ "0" ],
      :total  => [ "2" ],
      :limit  => [ "200" ]
    }
] }

Note how it puts everything insider an array, I suppose because it doesn't know if the schema allows multiple instances of, say, <offset>, inside <resultPage>. So, to access my data, I'm always adding a [0] at the end of everything.

Do I have to just live with this, or is there an elegant way around it?

John Bachir
  • 22,495
  • 29
  • 154
  • 227

1 Answers1

1

You can pass the "ForceArray" option into XmlSimple (it defaults to true, set it to false to get rid of the arrays).

 > XmlSimple.xml_in(str)
 => {"total"=>["2"], "offset"=>["0"], "limit"=>["200"]} 
 > XmlSimple.xml_in(str, {"ForceArray" => false})
 => {"total"=>"2", "offset"=>"0", "limit"=>"200"} 
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201