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.


In reply to Re: Re: Generate Perl Code from XML Schema? by Joost
in thread Generate Perl Code from XML Schema? by ayvazj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.