Reading in XML in Ruby with xmlsimple
Archive - Originally posted on "The Horse's Mouth" - 2016-05-18 20:56:28 - Graham EllisThe xmlsimple Ruby gem allows you to load in a file (or IO handle object or string) of XML as a multidimensional collection - hashes of arrays of hashes. There's a sample program here which includes this code to load the data from after __END__:
require 'xmlsimple'
info = XmlSimple.xml_in(DATA)
If I now the structure of what I'm getting, I can then access elements directly, for example:
p info["access"][0]["where"][0]
and if I don't know what the structure is, I can use Ruby to explore it. The source code at the link above contains a complete recursive piece of code for displaying a Hash that contains hashes, arrays and printable values to get you started!
There's an example using the alternative (standard) rexml library - [here].