Reputation: 25
I'm attempting to grab event log entries which are passed in xlm, convert them into a hash and then store into a database.
I'm currently using the XmlSimple gem to convert the xml input into a hash.
Test sample input:
require 'xmlsimple'
h = XmlSimple.xml_in('
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Service Control Manager" Guid="{555908d1-a6d7-4695-8e1e-26931d2012f4}" EventSourceName="Service Control Manager" />
</System>
</Event>
', { 'KeyAttr' => 'name' })
puts "#{h}"
This returns:
{"xmlns"=>"http://schemas.microsoft.com/win/2004/08/events/event", "System"=>[{"Provider"=>[{"Name"=>"Service Control Manager", "Guid"=>"{555908d1-a6d7-4695-8e1e-26931d2012f4}", "EventSourceName"=>"Service Control Manager"}]}]}
How can I access the System Provider GUID?
I can return all of the System elements by doing:
puts "#{h['System']}"
However
puts "#{h['System']['Provider'}"
Returns:
can't convert String into Integer (TypeError)
I've tried casting the result to a string with no luck. Do I have the XmlSimple formatting wrong? Am I accessing the wrong key? Is there another way to do this?
Thanks for any help!
Upvotes: 1
Views: 118
Reputation: 29599
be careful of the arrays
h["System"].first["Provider"].first["Guid"]
Upvotes: 1
Reputation: 7338
The []
at the beginning of the "System"
denotes that its value is an array of hashes. You can do this:
puts "#{h['System'][0]['Provider'}"
at the same time "Provider"
itself is an array, so you would have to do the same for it, for instance:
puts "#{h['System'][0]['Provider'][0]['Guid']"
Upvotes: 1
Reputation: 21
It looks like you're just missing a closing ]
. Hashes should nest arbitrarily deep without problem.
Upvotes: 1