Yo!
I have just released a new Ruby library called xml-simple. It offers
an easy API to maintain XML (especially configuration files) and was
heavily inspried by Grant McLean’s Perl module XML::Simple (Thank you
very much, Grant, for such great code!!).
Example:
Say you have a file of configuration options called foo.xml containing
this:
<config logdir="/var/log/foo/" debugfile="/tmp/foo.debug">
<server name="sahara" osname="solaris" osversion="2.6">
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</server>
<server name="gobi" osname="irix" osversion="6.5">
<address>10.0.0.102</address>
</server>
<server name="kalahari" osname="linux" osversion="2.0.34">
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</server>
</config>
The following lines of code:
require 'xmlsimple'
config = XmlSimple.xml_in('foo.xml', { 'key_attr' => 'name' })
will ‘slurp’ the configuration options into the Hash config (if no
arguments are passed to xml_in the name and location of the XML file
will be inferred from name and location of the script). You can dump
out the contents of the Hash using “p config”, which will produce
something like this (formatting has been adjusted for brevity):
{
'logdir' => '/var/log/foo/',
'debugfile' => '/tmp/foo.debug',
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [ '10.0.0.101', '10.0.1.101' ]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => [ '10.0.0.102' ]
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [ '10.0.0.103', '10.0.1.103' ]
}
}
}
Your script could then access the name of the log directory like this:
puts config['logdir']
Similarly, the second address on the server ‘kalahari’ could be
referenced as:
puts config['server']['kalahari']['address'][1]
What could be simpler? (Rhetorical).
You can find a short tutorial here:
http://www.maik-schmidt.de/projects/xml-simple/index.html
You can download the current and all previous releases here:
http://www.maik-schmidt.de/projects/xml-simple/dist/
The RAA site is:
http://www.ruby-lang.org/raa/list.rhtml?name=xml-simple
You can find the original Perl version here:
http://www.cpan.org/modules/by-module/XML/GRANTM/
Currently, I am adding all features of the Perl version, that did
not find their way into this release. Additionally, I will integrate
XmlSimple into XmlConfigFile.
Cheers,
···
–
Maik Schmidt
mailto:contact@maik-schmidt.de