[ANN] xml-simple 0.6.0

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

hi

i find it somewhat strange, that the “name”-attribute is a hash
containing the rest of the attributes as also the subnodes of this node.
do you take the first attribute to identify a node?

also it could possibly save a lot of finger-typing if you encapsulated
the “not so cool” hash in an object that supports XPath

puts config[‘server’][‘kalahari’][‘address’][1] # vs.

puts xml.get(‘/config/server/kalahari/address’)[1] # or
puts xml.get(‘/config/server(name=kalahari)/address’)[1] # best

just my suggestions.

  • Meinrad

Maik Schmidt wrote:

···

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,

Hi!

Similarly, the second address on the server ‘kalahari’ could be
referenced as:

            puts config['server']['kalahari']['address'][1]

What could be simpler? (Rhetorical).

That question is not as rhetorical as it may seem:

http://raa.ruby-lang.org/list.rhtml?name=xml-configfile

Josef ‘Jupp’ Schugt http://jupp.tux.nu jupp(AT)gmx(DOT)de

···


“Mankind must put an end to war before war puts an end to mankind.”
– John F. Kennedy

Maik Schmidt wrote:

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!!).
Hi,

I downloaded the zip file (twice) and get, on
unzip -l tmp/xml-simple_0.6.0.zip :

Archive: /tmp/xml-simple_0.6.0.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of /tmp/xml-simple_0.6.0.zip or
/tmp/xml-simple_0.6.0.zip.zip, and cannot find
/tmp/xml-simple_0.6.0.zip.ZIP, period.

This is unzip from unzip-5.50 from www.info-zip.org on Linux.

Any ideas ?

Cheers,

Han

Meinrad Recheis wrote:

···

i find it somewhat strange, that the “name”-attribute is a hash
containing the rest of the attributes as also the subnodes of this node.
do you take the first attribute to identify a node?

No, you can control the structure by using the option ‘key_attr’ as
described extensively in the tutorial. E.g., you can disable the
folding completely by setting ‘key_attr’ = (the default). Maybe
you want to take another look at the manual and give ‘key_attr’ a
chance :wink:

also it could possibly save a lot of finger-typing if you encapsulated
the “not so cool” hash in an object that supports XPath

puts config[‘server’][‘kalahari’][‘address’][1] # vs.

puts xml.get(‘/config/server/kalahari/address’)[1] # or
puts xml.get(‘/config/server(name=kalahari)/address’)[1] # best

That is really funny, because what you suggest here can be found
in another Ruby library I wrote: XmlConfigFile. Get it from RAA:
http://raa.ruby-lang.org/list.rhtml?name=xml-configfile
It allows you to access configuration parameters in an XML file
using XPath. But many people asked for an easy way to completely
serialize an XML document and so I decided to port XML::Simple to
Ruby. I want to combine these two libraries in the next weeks, i.e.
XmlConfigFile will use XmlSimple for serializing purposes.

just my suggestions.

Thank you! They are always very welcome!

The author must find this really flattering :slight_smile:

Project name: xml-configfile
Short description: Easy handling of XML configuration files.
Owner: Maik Schmidt

Project name: xml-simple
Short description: Easy API to maintain XML
Owner: Maik Schmidt

···

On Mon, Mar 31, 2003 at 10:32:51PM +0900, Josef ‘Jupp’ Schugt wrote:

Hi!

Similarly, the second address on the server ‘kalahari’ could be
referenced as:

            puts config['server']['kalahari']['address'][1]

What could be simpler? (Rhetorical).

That question is not as rhetorical as it may seem:

http://raa.ruby-lang.org/list.rhtml?name=xml-configfile


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

People disagree with me. I just ignore them.
– Linus Torvalds, regarding the use of C++ for the Linux kernel

Han Holl wrote:

I downloaded the zip file (twice) and get, on
unzip -l tmp/xml-simple_0.6.0.zip :

Archive: /tmp/xml-simple_0.6.0.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of
/tmp/xml-simple_0.6.0.zip or
/tmp/xml-simple_0.6.0.zip.zip, and cannot find
/tmp/xml-simple_0.6.0.zip.ZIP, period.

This is unzip from unzip-5.50 from www.info-zip.org on Linux.

Any ideas ?

I’m sorry! For a reason that is still a mystery to me, my FTP
client assumed the zip archive to be an ASCII file and turned it
into a pile of “byte rubbish” while uploading it. Usually I download
the file again from my web site and check, if everything is fine.
This time I forgot it! Damn!!

I have forced my FTP client to use binary mode now and if you try to
download it again, everything should work fine!

Sorry for the inconvenience!

Cheers,