in reply to Re: Generate Perl Code from XML Schema?
in thread Generate Perl Code from XML Schema?

Depending on exactly what you want to do with the generated "code" (all I can see in your example are some accessors) and provided your XML files are reasonably small, you might be able to get away with XML::Simple. This will NOT generate code or classes from schemas, but it WILL generate a nice Perl-y datastructure from XML files.

From the docs:

Say you have a script called foo and a file of configuration options c +alled foo.xml containing this: </p> <code> <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 in foo:

use XML::Simple; my $config = XMLin();

will 'slurp' the configuration options into the hashref $config (because no arguments are passed to XMLin() 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 hashref using Data::Dumper:

use Data::Dumper; print Dumper($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'Does that seem like what => '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:

print $config->{logdir};

similarly, the second address on the server 'kalahari' could be referenced as:

print $config->{server}->{kalahari}->{address}->[1];
That seems to be what you want from where I'm standing, anyway :-)

Joost.