rubyxmlyamlrexml

converting from xml name-values into simple hash


I don't know what name this goes by and that's been complicating my search.

My data file OX.session.xml is in the (old?) form

<?xml version="1.0" encoding="utf-8"?>
<CAppLogin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://oxbranch.optionsxpress.com">
  <SessionID>FE5E27A056944FBFBEF047F2B99E0BF6</SessionID>
  <AccountNum>8228-5500</AccountNum>
  <AccountID>967454</AccountID>
</CAppLogin>

What is that XML data format called exactly?

Anyway, all I want is to end up with one hash in my Ruby code like so:

CAppLogin = { :SessionID => "FE5E27A056944FBFBEF047F2B99E0BF6", :AccountNum => "8228-5500", etc. }   # Doesn't have to be called CAppLogin as in the file, may be fixed

What might be shortest, most built-in Ruby way to automate that hash read, in a way I can update the SessionID value and store it easily back into the file for later program runs?

I've played around with YAML, REXML but would rather not yet print my (bad) example trials.


Solution

  • There are a few libraries you can use in Ruby to do this.

    Ruby toolbox has some good coverage of a few of them:

    https://www.ruby-toolbox.com/categories/xml_mapping

    I use XMLSimple, just require the gem then load in your xml file using xml_in:

    require 'xmlsimple'
    hash = XmlSimple.xml_in('session.xml')
    

    If you're in a Rails environment, you can just use Active Support:

    require 'active_support' 
    session = Hash.from_xml('session.xml')